networking,

Tunnels, Keys and Handshakes: A Practical Guide to IPsec VPN

IKEv2, Phase 1, Phase 2, ESP, traffic selectors, NAT-T, PFS: what every IPsec setting actually does, which algorithms to pick in 2026, and a step-by-step way to debug a tunnel that won't come up.

Monday morning. The branch office calls: the ERP system is unreachable over the VPN. You log into the firewall and the tunnel says down. In front of you is a wall of settings: IKEv2, AES-256-GCM, DH Group 14, Phase 1, Phase 2, Proxy IDs, PFS.

Where do you even start?

This guide takes IPsec apart one piece at a time. By the end you’ll have a mental model that makes both configuration and troubleshooting feel a lot less like guesswork.


IPsec in One Minute

IPsec (Internet Protocol Security) is a suite of protocols that secures traffic at Layer 3, the network layer.

That is the big difference to TLS. TLS protects one application connection, like a single HTTPS session. IPsec protects every packet between two networks or hosts, no matter which application sent it.

You’ll find it in three places:

  • Site-to-site tunnels that connect offices.
  • Remote-access VPNs for people working on the road.
  • Cloud connectivity: AWS, Azure and GCP all offer IPsec tunnels to on-premises networks.

Before we dive in, here are the five terms that the rest of the article builds on:

TermWhat it means
IKEInternet Key Exchange. The protocol that negotiates everything before data flows.
SASecurity Association. A “contract” of agreed algorithms, keys and lifetimes.
IKE SAThe SA for the control channel (Phase 1).
Child SAThe SA for the actual data tunnel (Phase 2), also called the IPsec SA.
ESPEncapsulating Security Payload. The protocol that encrypts your packets.

The Big Picture: Two Phases

Two peers never start encrypting right away. First they agree on how to protect the traffic. IKE handles this negotiation in two phases:

  1. Phase 1 builds a secure, authenticated control channel (the IKE SA).
  2. Phase 2 uses that channel to build the data tunnel (the Child SA).
flowchart LR
    P1["Phase 1: IKE SA
control channel
who are you?
which keys?"] --> P2["Phase 2: Child SA
data tunnel
which traffic?
which crypto?"] P2 --> T(["🔒 ESP traffic
Site A ⇄ Site B"])

IKEv2 does it in four messages

In IKEv2, which is what you should be running, the whole setup takes just two request/response pairs. The trick is that the first Child SA is negotiated inside the IKE_AUTH exchange (RFC 7296).

sequenceDiagram
    autonumber
    participant A as Site A (initiator)
    participant B as Site B (responder)

    rect rgba(46, 139, 87, 0.08)
        Note over A,B: IKE_SA_INIT (in the clear)
        A->>B: algorithm proposals
DH public value + nonce B->>A: chosen algorithms
DH public value + nonce Note over A,B: both compute the same secret
from here on, everything is encrypted end rect rgba(46, 139, 87, 0.16) Note over A,B: IKE_AUTH (encrypted) A->>B: identity + proof (PSK or certificate)
Child SA proposal + traffic selectors B->>A: identity + proof
accepted Child SA end A-->>B: 🔒 encrypted traffic B-->>A: 🔒 encrypted traffic Note over A,B: later rekeys and extra tunnels
use CREATE_CHILD_SA

Note: IKEv1 needed six messages for Phase 1 (Main Mode) plus three for Phase 2. Its shortcut, Aggressive Mode, sends the peer identity in the clear before authentication. The IETF declared all of IKEv1 “historic” in April 2023 (RFC 9395). Use IKEv2 for anything new.

What each phase negotiates

Phase 1 gets its secret from a Diffie-Hellman (DH) exchange. Both sides send a public value, and both compute the same shared secret without ever sending it over the wire.

Phase 2 then picks its own, independent algorithm set for your data. Here is how the two compare:

Phase 1: IKE SAPhase 2: Child SA
JobSecure control channelProtect your data
Encryptione.g. AES-256-CBCe.g. AES-256-GCM
Integrity / PRFe.g. SHA-256built into GCM
Key exchangeDH group (e.g. 19)optional fresh DH (PFS)
Also agrees onAuthentication: PSK or certificateProtocol (ESP) and traffic selectors
Typical lifetime24 hours1 hour or a byte limit

How Packets Get Protected

IPsec gives you two choices of protocol and two choices of mode.

ESP, not AH

ESP encrypts and authenticates the packet payload. It deliberately leaves the outer IP header out of its integrity check, so NAT devices can rewrite addresses without breaking anything.

AH (Authentication Header) authenticates but does not encrypt. Worse, its integrity check covers the outer IP addresses. NAT changes exactly those addresses, so the receiver’s check fails and the packet is dropped.

Almost every real path has a NAT device somewhere. So the rule is simple: always use ESP, ideally with an AEAD cipher like AES-GCM that does encryption and integrity in one pass.

Tunnel mode vs. transport mode

Tunnel mode wraps the entire original packet in a new one. Your ISP only sees traffic between the two firewall IPs; the real source and destination are hidden inside. This is what site-to-site and remote-access VPNs use.

Transport mode keeps the original IP header and only protects the payload. It is for host-to-host encryption between two servers and is rare in VPNs.

original packet
IP 10.0.1.5 → 10.0.2.10 TCP + data
tunnel mode (site-to-site)
IP FW-A → FW-B ESP IP 10.0.1.5 → 10.0.2.10 TCP + data trailer
transport mode (host-to-host)
IP Host A → Host B ESP TCP + data trailer
What ESP adds in each mode: an ESP header in front, a trailer with the integrity check behind. In tunnel mode even the internal addresses are hidden.

Proving Identity: PSK or Certificates

Phase 1 can only finish once both peers have proven who they are. There are two ways to do it.

Pre-shared key (PSK)Certificates (PKI)
How it worksSame secret configured on both sidesEach peer proves it holds the private key for a CA-signed certificate
SetupQuickNeeds a CA
At scale50 tunnels = 50 secrets to rotateOne CA, central revocation
If it leaksThat tunnel is fully compromisedRevoke one certificate
Best forA handful of static tunnelsEnterprises, remote access

Traffic Selectors: What Goes Through the Tunnel?

This is the setting that trips people up most, and it causes a big share of all tunnel failures.

Traffic Selectors (the IKEv2 term) and Proxy IDs (the IKEv1 and vendor term) are the same thing. They define which traffic the tunnel carries: a local IP range, a remote IP range, and optionally protocol and port.

Local:    10.10.1.0/24   (your network)
Remote:   10.20.1.0/24   (their network)
Protocol: 0 (any)
Port:     0 (any)

Think of a secure freight corridor between two warehouses. If warehouse A says “I’m sending bays 1 to 5” and warehouse B expects “bays 1 to 7”, the shipment is refused at the door. The corridor itself is fine.

Both sides must offer mirror images of each other. Even a subtle difference kills Phase 2:

flowchart TB
    subgraph OK["✅ Match: Phase 2 comes up"]
        direction LR
        A1["Site A
local 10.0.1.0/24
remote 10.0.2.0/24"] <--> B1["Site B
local 10.0.2.0/24
remote 10.0.1.0/24"] end subgraph FAIL["❌ Mismatch: Phase 2 fails"] direction LR A2["Site A
local 10.0.1.0/24
remote 10.0.2.0/24"] <-. "✗" .-> B2["Site B
local 10.0.2.0/25
remote 10.0.1.0/24"] end OK ~~~ FAIL

Policy-based vs. route-based VPNs

Policy-basedRoute-based
What decides the tunnel trafficA firewall policy listing subnetsThe routing table, via a virtual tunnel interface (VTI)
Traffic selectorsSpecific subnets on both sidesUsually 0.0.0.0/0 on both sides
Adding a subnetChange the VPN on both endsAdd a route
Dynamic routing (BGP, OSPF)AwkwardNatural fit

Route-based VPNs sidestep most selector headaches. That is why they dominate cloud connectivity and SD-WAN today.


Picking Algorithms in 2026

Weak crypto undermines the whole point of the tunnel. Remember that Phase 1 and Phase 2 have separate algorithm sets, so check both.

✅ Use⚠️ Phase out❌ Avoid
EncryptionAES-256-GCM, AES-128-GCM (ESP); AES-256-CBC (IKE)3DES, DES
Integrity / PRFSHA-256, SHA-384SHA-1MD5
DH group19, 20 (ECDH P-256/P-384); 14 (2048-bit) as minimum5 (1536-bit)1, 2 (768/1024-bit)

A few things worth knowing behind that table:

  • AES-CBC in Phase 1, AES-GCM in Phase 2. RFC 8247 makes AES-CBC the MUST for IKE and GCM only a SHOULD, because not every vendor supports GCM for the IKE SA. CBC is the safe, interoperable choice there. For ESP, GCM is the clear winner.
  • GCM needs no separate HMAC. It is an AEAD cipher (Authenticated Encryption with Associated Data). Don’t configure an extra integrity algorithm next to it.
  • PRF vs. integrity. The integrity algorithm checks each message. The PRF (pseudo-random function) derives the keys from the DH secret. Both use the SHA-2 family, and neither should ever be MD5 or SHA-1.
  • DH groups 1 and 2 are broken in practice. The 2015 Logjam research broke 512-bit DH in minutes and showed 1024-bit is within reach of a nation-state. Group 14 is the RFC 8247 minimum.
  • 3DES is not “deprecated”, it is disallowed. NIST disallowed it for encryption after 31 December 2023. Its 64-bit block size makes the Sweet32 birthday attack (CVE-2016-2183) practical once a few dozen gigabytes travel under one key.

NAT Traversal: The Port 4500 Trap

ESP is IP protocol 50. It has no port numbers, so NAT devices can’t track it like TCP or UDP sessions.

NAT-Traversal (NAT-T) (RFC 3948) fixes this. During IKE_SA_INIT, each peer sends a hash of its own IP address and port. If what arrives differs, a NAT device rewrote it. Both peers then move everything to UDP port 4500.

flowchart TD
    S["IKE_SA_INIT on UDP 500
+ NAT detection hashes"] --> Q{"Hashes match
what arrived?"} Q -->|"yes, no NAT"| N["IKE stays on UDP 500
ESP runs as IP protocol 50"] Q -->|"no, NAT found"| T["IKE_AUTH and all ESP
move to UDP 4500"] T --> W["⚠️ UDP 4500 blocked?
tunnel silently fails"]

NAT-T is built into IKEv2 and you rarely configure it. What you must do is open the right ports on every firewall and cloud security group in the path:

Port / protocolNeeded for
UDP 500IKE negotiation, always
UDP 4500IKE and ESP when NAT is detected
IP protocol 50ESP when there is no NAT

Warning: A classic failure is a cloud security group that allows UDP 500 but not UDP 4500. Negotiation starts, NAT is detected, the traffic switches ports and vanishes.


Keys That Expire: Lifetimes and PFS

Every SA has a lifetime. When it runs out, the peers negotiate fresh keys. That is a feature: if a key ever leaks, only the traffic from that window is exposed.

  • Phase 1 typically lives 24 hours (86,400 s).
  • Phase 2 typically lives 1 hour (3,600 s), often with a byte limit (e.g. 4 GB) too. Whichever comes first triggers the rekey.

Badly mismatched lifetimes on the two peers can create a ghost tunnel: one side has torn down the SA, the other still shows it as up, and traffic silently disappears. Dead Peer Detection (DPD) helps catch this.

Perfect Forward Secrecy

Without PFS, Phase 2 keys are derived from the Phase 1 key material. Whoever gets the Phase 1 keys could decrypt recorded Phase 2 traffic.

With PFS, every Phase 2 negotiation runs its own fresh DH exchange. Each set of keys stands alone, so cracking one reveals nothing about the others.

flowchart TB
    subgraph NOPFS["Without PFS"]
        direction TB
        K1["Phase 1 keys"] --> C1["Child SA keys #1"]
        K1 --> C2["Child SA keys #2"]
        K1 --> C3["Child SA keys #3"]
    end
    subgraph PFS["With PFS"]
        direction TB
        D1["fresh DH"] --> P1["Child SA keys #1"]
        D2["fresh DH"] --> P2["Child SA keys #2"]
        D3["fresh DH"] --> P3["Child SA keys #3"]
    end
    NOPFS ~~~ PFS

The cost is a little extra CPU at each rekey. For anything sensitive, that is an easy call. Use at least the same DH group as Phase 1.


A Reference Configuration

This is a solid baseline for a site-to-site IKEv2 tunnel on Palo Alto, Fortinet, Cisco or similar. The menus differ by vendor; the parameters don’t.

Phase 1 (IKE SA)

ParameterValueWhy
IKE versionIKEv2IKEv1 is historic (RFC 9395)
EncryptionAES-256-CBCRFC 8247 MUST; GCM if both ends support it
Integrity / PRFSHA-256 or SHA-384MD5 is MUST NOT, SHA-1 is deprecated
DH group19 or 20 (14 minimum)Group 14 is the RFC 8247 minimum
AuthenticationCertificate (or PSK)Certificates scale better
Lifetime86,400 s
Dead Peer DetectionOnTears down stale SAs, prevents ghost tunnels

Phase 2 (Child SA)

ParameterValueWhy
ProtocolESPAH breaks behind NAT
EncryptionAES-256-GCMAEAD, no separate integrity needed
IntegritySHA-256 only if not using GCM
PFSOn, DH group 14 or higherSame or stronger than Phase 1
Lifetime3,600 s and 4 GBWhichever triggers first
Traffic selectorsSpecific subnetsAvoid 0.0.0.0/0 on policy-based tunnels

Firewall rules people forget

RuleDetail
Allow UDP 500IKE
Allow UDP 4500NAT-T
Allow IP protocol 50Only if there is no NAT in the path
NAT exemptionExclude VPN traffic from outbound NAT

When the Tunnel Is Down: A Troubleshooting Map

Most failures fall into a handful of patterns. Work through them in order, because each step only makes sense once the previous one is fine.

flowchart TD
    START(["tunnel not working"]) --> P1{"Phase 1 up?"}
    P1 -->|no| F1["ports 500 / 4500
crypto + DH group
PSK or certificate
peer ID"] P1 -->|yes| P2{"Phase 2 up?"} P2 -->|no| F2["traffic selectors
PFS group"] P2 -->|yes| P3{"traffic
flowing?"} P3 -->|no| F3["routing
selectors
NAT exemption"] P3 -->|"yes, but drops"| F4["lifetimes
DPD
rekey margin"]

What to look for on each branch:

  • Phase 1 fails. Almost always a mismatch in ports, algorithms, DH group or credentials. A very common gotcha: one side identifies itself by IP address and the other expects a hostname.
  • Phase 2 fails. Almost always the traffic selectors: compare proposed vs. received selectors in the logs. A /24 on one side and a /25 on the other is enough. A mismatched PFS group fails here too.
  • Up but no traffic. Check routing first. Then check that VPN traffic is exempt from outbound NAT; otherwise the source IP gets translated and no longer matches the selectors.
  • Intermittent drops. A rekey gap: some vendors overlap old and new SAs, others leave a short gap. Align lifetimes, enable DPD and look for “rekey margin” or “rekey fuzz” settings.
  • Peer behind 1:1 NAT. Configure its public IP as the peer address, not the private one.

Key Takeaways

  • Two phases. Phase 1 builds the control channel, Phase 2 the data tunnel. IKEv2 does both in four messages.
  • IKEv2 only. IKEv1 has been historic since 2023.
  • ESP in tunnel mode. AH breaks behind NAT.
  • AES-CBC for IKE, AES-GCM for ESP. DH group 14 at minimum, 19 or 20 preferred.
  • Avoid DES, 3DES, MD5 and DH groups 1 and 2.
  • Traffic selectors must mirror each other, or use a route-based VPN.
  • Open UDP 4500, not just UDP 500.
  • Enable PFS and DPD, and keep lifetimes aligned to avoid ghost tunnels.

Your Audit Checklist

If you run IPsec tunnels today, give them a quick pass:

  1. Any IKEv1 left? Migrate to IKEv2. At the very least, disable Aggressive Mode now.
  2. Any weak crypto? Hunt for 3DES, DES, MD5 and DH groups below 14 in both phases (NIST SP 800-131A Rev. 2).
  3. Old PSKs? Rotate any key older than a year, or move to certificates.
  4. Long Phase 2 lifetimes? 24 hours or more is too long. Add a byte limit for busy tunnels.
  5. Cloud gateways with only UDP 500 open? Add UDP 4500.

And the next time someone calls on a Monday morning, you’ll know exactly where to look.


Sources and further reading:

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