IPsec is the framework that turns the open internet into a private link. It is not one protocol but a kit — two data-plane protocols, a key-exchange protocol with a life of its own, and a pile of negotiable algorithms. Most IPsec troubleshooting is really one question: which of those negotiations failed? This walk-through goes from the four services IPsec provides to IKEv2, SPIs, and the NAT problem that breaks tunnels in the field.
1. The four services, and two ways to deliver them
IPsec provides confidentiality (encryption), integrity (the packet was not altered), authentication (it came from the real peer), and anti-replay (an attacker cannot resend captured packets). Two protocols carry them: AH (protocol 51) authenticates but does not encrypt, and ESP (protocol 50) encrypts and authenticates. In practice everyone uses ESP — AH’s lack of encryption and its incompatibility with NAT retired it.
2. Transport mode vs tunnel mode
IPsec protects a packet in one of two shapes. Transport mode encrypts only the payload and keeps the original IP header — host-to-host, where the endpoints are the crypto endpoints. Tunnel mode wraps the entire original packet inside a new IP header — the shape every site-to-site VPN uses, because the gateways encrypt on behalf of the hosts behind them, hiding the internal addresses.
3. IKE builds the tunnel in phases
Before any data is protected, the peers must agree on keys and algorithms — that is IKE‘s job, and it runs over UDP 500. IKEv1 does it in two phases: Phase 1 builds a secure management channel (the ISAKMP SA) and Phase 2 negotiates the actual IPsec SAs that protect traffic. IKEv2 collapses this into far fewer messages, but the mental model — secure the negotiation first, then negotiate the data protection — is the same.
4. Phase 1 — Diffie-Hellman and peer authentication
Phase 1 solves the hardest problem: agreeing on a secret key over a line an attacker is watching. Diffie-Hellman lets both sides derive a shared key by exchanging only public values — the eavesdropper sees the exchange but cannot compute the key. The DH group sets its strength. The peers then prove who they are with a pre-shared key or certificates; a PSK mismatch is the single most common Phase 1 failure.
5. Security Associations and the SPI
The negotiated result is a Security Association (SA) — a bundle of keys, algorithms, and lifetime. A crucial detail: IPsec SAs are unidirectional, so every tunnel has at least two, one per direction. Each is identified by a Security Parameter Index (SPI) carried in the packet, which tells the receiver which SA — which key set — to use to decrypt. When you read show crypto output, you are reading SAs and SPIs.
6. What an ESP packet actually looks like
Seeing the ESP encapsulation makes the rest concrete. ESP adds a header (with the SPI and a sequence number for anti-replay) in front of the protected data, and a trailer plus an authentication value behind it. The original payload — and in tunnel mode the original IP header too — sits encrypted in the middle, unreadable without the SA’s key.
7. IKEv2 — why the newer version wins
IKEv2 is not a tweak; it is a cleaner protocol. It establishes a tunnel in a single four-message exchange instead of IKEv1’s six-plus-three, so it sets up faster. It builds in NAT traversal, dead-peer detection, and EAP authentication that IKEv1 bolted on awkwardly. It also reliably handles rekeying and asymmetric authentication. For anything new, IKEv2 is the default; IKEv1 lingers only for legacy interoperability.
8. The three things that break tunnels in the field
Three real-world mechanisms cause most production incidents. NAT-T: ESP has no port numbers, so a NAT device cannot track it; IKEv2 detects NAT and wraps ESP in UDP 4500 so it survives — if that is blocked, the tunnel builds but no traffic passes. DPD (dead-peer detection) notices a silently dead peer and tears the SA down instead of blackholing traffic. PFS (perfect forward secrecy) forces a fresh Diffie-Hellman at each rekey, so compromising one key does not expose past or future sessions — and a PFS mismatch between peers silently drops rekeys.
Takeaways
IPsec is a negotiation wrapped in a negotiation: IKE agrees how to protect the agreement, then agrees how to protect your data, producing unidirectional SAs selected by SPI. Know ESP over AH, tunnel over transport for VPNs, IKEv2 over IKEv1, and the NAT-T/DPD/PFS trio — and the mysterious tunnel that builds but passes no traffic stops being mysterious.