Sub topic 8
IPsec: ESP, IKE and the two modes
This chapter assumes you know how encapsulation works, how a routing table selects a route, and the basics of keys and certificates from the earlier chapters. It covers how IPsec protects traffic at the IP layer, which parts do what, and which mistakes come up most often.
The working model
IPsec is a set of protocols that adds authentication and encryption to IP packets. It works below TCP and UDP, so applications do not need to know it exists. Two parts do the work:
- ESP (Encapsulating Security Payload) protects the data packets. It provides confidentiality, integrity and origin authentication. ESP is IP protocol number 50.
- IKE (Internet Key Exchange) is the control protocol. It authenticates the peers, agrees on keys and sets up the ESP connections. IKEv2 runs over UDP port 500.
An older protocol, AH (Authentication Header, protocol 51), only authenticates. It cannot encrypt and breaks when a NAT changes the addresses it covers, so modern deployments use ESP.
Security associations
A security association (SA) is a one-way agreement that records the algorithms, keys and lifetime for one direction of traffic. Each SA is identified by a 32-bit SPI (Security Parameter Index) carried in every ESP packet. The receiver uses the SPI to find the right keys.
IKE creates two kinds of SA:
- The IKE SA protects the IKE messages themselves. Peers authenticate here, using a pre-shared key or certificates, and run a Diffie-Hellman exchange.
- One or more child SAs (also called IPsec SAs) carry the actual traffic. Each direction has its own SA, so a normal tunnel has a pair.
The child SAs are rekeyed on a timer or after a volume of data. The IKE SA stays up and builds new keys without dropping traffic.
Tunnel mode and transport mode
The two modes differ in what ESP wraps.
| Property | Transport mode | Tunnel mode |
|---|---|---|
| What is protected | The payload (TCP or UDP data) | The whole original IP packet |
| Outer IP header | The original header is kept | A new header is added |
| Typical use | Host to host, or protecting an existing tunnel such as L2TP | Site-to-site and remote access VPNs |
| Hides inner addresses | No | Yes |
In tunnel mode the original packet becomes the encrypted payload, and a new outer header carries the gateway addresses. This is encapsulation, as in GRE, with encryption added. Gateways can then forward private subnets such as 10.1.0.0/16 across the internet.
Traffic selectors
IKE negotiates traffic selectors: the source and destination ranges that a child SA may carry. If one side says 10.1.0.0/16 to 10.2.0.0/16 and the other says 10.1.0.0/24 to 10.2.0.0/16, negotiation narrows or fails. Packets that match no selector are not protected and follow the normal routing table.
Common errors
- Mismatched proposals. Both sides must share at least one encryption, integrity and Diffie-Hellman group. The logs show
NO_PROPOSAL_CHOSEN. - Mismatched selectors. The IKE SA comes up but no child SA does, or traffic never matches.
- Wrong identities or keys. Authentication fails during
IKE_AUTH. - Blocked ports. Firewalls must allow UDP 500, UDP 4500 (used when NAT is detected) and ESP.
- MTU. ESP adds overhead, so large packets may fail unless you lower the MTU or clamp the TCP MSS.
Exercise
An IPsec tunnel shows the IKE SA as established, but pings between the two private subnets get no reply and ESP counters do not increase. Name two likely causes and where you would look.
Show answer
The traffic selectors may not match the subnets being pinged, so no child SA covers the traffic. Check the child SA and selectors in the IKE daemon logs and in ip xfrm policy. Alternatively, a route may be missing so the packets never reach the tunnel policy, or a firewall may drop ESP or UDP 4500. Check ip route get for the destination and confirm the firewall rules.
Exercise
Why does tunnel mode hide the inner source and destination addresses while transport mode does not?
Show answer
Tunnel mode encrypts the whole original IP packet, including its header, and adds a new outer header with the gateway addresses. Transport mode keeps the original header and encrypts only the payload.