01 IPsec and IKE (Phases, SAs and Selectors)

IPsec is a set of protocols that encrypts and authenticates IP traffic between two points. IKE is the protocol that negotiates IPsec’s parameters and keys. Keeping the two apart explains almost every site-to-site tunnel failure: something fails while negotiating (IKE) or while carrying the traffic (ESP).

Contenido complementario

Not covered in my courses. Verified against the RFCs listed in Sources. Commands are in IPsec Fundamentals (Palo Alto), IPSec - Configuración (Cisco) and Non-Meraki VPN Peers (IPsec) (Meraki).

The two data protocols

  • ESP (IP protocol 50): encrypts and authenticates the payload. This is the one used in practice.
  • AH (IP protocol 51): authenticates only, no encryption. Rarely used, and it does not pass through NAT.
  • Tunnel mode: the whole original packet is encapsulated behind a new IP header. This is the mode used by site-to-site VPNs.
  • Transport mode: only the payload is protected; used end to end between two hosts.

IKE: two phases

Phase 1 (IKE SA)Phase 2 (IPsec SA / Child SA)
What it buildsA secure control channel between the peersThe tunnels that encrypt the real traffic
What it negotiatesEncryption, hash, DH group, authentication (PSK or certificates), lifetimeESP encryption and hash, PFS, lifetime, which traffic is protected (selectors)
PortsUDP 500 (UDP 4500 with NAT-T)Inside the phase 1 channel

An SA (Security Association) is a one-way agreement identified by an SPI. That is why a tunnel has at least two data SAs (one per direction).

IKEv1 vs IKEv2

IKEv1IKEv2
Initial messagesMain mode: 6 + Quick mode: 34 (IKE_SA_INIT ×2 + IKE_AUTH ×2)
Phase 1 modesMain (protects identities) and AggressiveA single exchange flow
Selectors per SAOne source/destination pairCan be lists of ranges (narrowing)
NAT-TExtensionBuilt in
Dead peer detectionDPD (extension)Built-in liveness check
AuthenticationPSK or certificatesPSK, certificates, EAP

Same word, different behavior

Even though the IKEv2 standard allows several subnets per SA, some devices keep the IKEv1 limit (one pair per SA). That difference is a real cause of “only one subnet works” between different vendors.

Selectors (traffic selectors / proxy IDs)

They are the list of subnets (source and destination) that each SA protects. For phase 2 to come up, the selectors on one side must be compatible with the other side’s. On policy-based devices they are written by hand; on route-based devices they are usually 0.0.0.0/0 ↔ 0.0.0.0/0 and the routes decide what enters the tunnel (see 02 - Policy-based vs Route-based VPN).

How IKEv2 negotiates selectors (RFC 7296 §2.9)

  • Each Traffic Selector payload carries one or more selectors, each with an address range, a port range and an IP protocol.
  • The responder may narrow what was proposed, but may not return an empty set.
  • If nothing proposed is acceptable, it answers TS_UNACCEPTABLE. If it accepts only one pair, it can answer SINGLE_PAIR_REQUIRED (this explains why some devices “accept only one pair” in IKEv2 as well).
  • When rekeying, a new Child SA cannot have narrower selectors than the original one (§2.9.2).

IKEv1 modes and messages (RFC 2409)

  • Main mode: 6 messages; protects the peers’ identities.
  • Aggressive mode: 3 messages; with PSK it does not protect the identities.
  • Quick mode (phase 2): 3 messages; negotiates the data SAs and, optionally, the client identities (IDci/IDcr, the equivalent of proxy IDs). Without them, the identities are the peers’ IP addresses.
  • PFS: basic Quick mode doesn’t provide it; you get it by including a Diffie-Hellman exchange (KE) in phase 2.

Lifetimes and rekey

  • IKEv1: the initiator proposes the lifetime and the responder cannot modify the attributes of the offer; it can only choose among the offered transforms.
  • IKEv2: the lifetime is not negotiated; “each end of the SA is responsible for enforcing its own lifetime policy” (RFC 7296 §2.4 and §2.8). Whichever side expires first starts the rekey.
  • In practice: even though the protocol tolerates different lifetimes, in production you should match them, because a mismatch is a known cause of periodic tunnel drops (see Troubleshooting VPN Meraki).

NAT-T and dead peer detection

  • NAT-T (RFC 3948): ESP is encapsulated in UDP 4500. AH was left out of the specification because it protects the outer IP addresses and conflicts with NAT. A keepalive is a UDP packet with a single 0xFF byte, used only to keep the NAT mapping alive.
  • In IKEv2 both peers send NAT_DETECTION_SOURCE_IP and NAT_DETECTION_DESTINATION_IP in IKE_SA_INIT; if the hashes don’t match there is NAT, and the initiator moves to UDP 4500 (§2.23).
  • DPD (IKEv1, RFC 3706): announced with a vendor ID; it does not send probes on a fixed schedule, only after idle time when outbound traffic gets no answer. In IKEv2 an empty INFORMATIONAL message works as the liveness check (§2.4).

Self-check

Q1 — What is the minimum number of data SAs in an IPsec tunnel?

A) 1, because it is a single tunnel
B) 2, one per direction
C) 4, one per phase and per direction
D) It depends only on the lifetime

Q2 — Which protocol and port does NAT-T use to cross NAT?

A) Plain ESP, protocol 50
B) AH over UDP 500
C) ESP encapsulated in UDP 4500
D) GRE over TCP 443

Q3 — How many messages does IKEv2 need to build the IKE SA and the first Child SA?

A) 2
B) 4
C) 6
D) 9

Q4 — Phase 1 comes up but phase 2 fails with a selector error. What do you check first?

A) The PSK
B) The phase 1 DH group
C) That the protected subnets match on both sides, including the mask
D) The PAN-OS version

Q5 — In IKEv2, what happens to SA lifetimes?

A) They are negotiated and the lowest wins
B) The initiator sets them and the responder must accept them
C) They are not negotiated: each side enforces its own and whichever expires first rekeys
D) They are always 8 hours

Q6 — An IKEv2 responder receives selectors that are too broad and accepts only one subnet pair. Which message can it return?

A) AUTHENTICATION_FAILED
B) SINGLE_PAIR_REQUIRED
C) Always NO_PROPOSAL_CHOSEN
D) Nothing, it silently drops the packet

Sources