L3 · security · reviewed

IPsec

Internet Protocol Security

ESP (or AH) protects packets under SAs negotiated by IKEv2; the SPD decides what enters, the SAD holds the keys.

Presenter modeEmbed this figure

How IPsec WorksRFC 4301 · PROPOSED STANDARD · December 2005The Security Policy Database (SPD)RFC 4301 · PROPOSED STANDARD · December 2005Encapsulating Security Payload Packet FormatRFC 4303 · PROPOSED STANDARD · December 2005The Initial ExchangesRFC 7296 · INTERNET STANDARD · October 2014IntroductionRFC 4302 · PROPOSED STANDARD · December 2005

Why it exists

IP forwards packets without confidentiality or integrity. IPsec is a framework — ESP or AH for packet protection, IKE for keying, and databases that decide what must be protected — so two peers can agree policy without trusting the path between them. How IPsec WorksRFC 4301 · PROPOSED STANDARD · December 2005

Most operational pain is not “encryption failed” but a mismatch among those pieces: selectors, mode, lifetimes, or proposal algorithms. The Security Policy Database (SPD)RFC 4301 · PROPOSED STANDARD · December 2005

IKE builds the Child SA, ESP carries the traffic

IKEv2 negotiates an IKE SA then a Child SA. ESP uses the Child SA’s SPI to protect packets that match the traffic selectors.

Left sends IKE_SA_INIT. The peers agree Diffie-Hellman and cryptographic proposals, then authenticate in IKE_AUTH and create a Child SA with matching traffic selectors. Left · initiator: IKE SA negotiating. Right · responder. Host A · 10.1.1.10. Host B · 10.2.2.20.

Left · initiatorIKE SA: negotiatingRight · responderHost A · 10.1.1.10Host B · 10.2.2.20
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
Left · initiatorrouterIKE SA: negotiating
Right · responderrouter
Host A · 10.1.1.10host
Host B · 10.2.2.20host
Left · initiatorRight · responderlinkup
Host A · 10.1.1.10Left · initiatorlinkup
Host B · 10.2.2.20Right · responderlinkup
1 / 4

Left sends IKE_SA_INIT. The peers agree Diffie-Hellman and cryptographic proposals, then authenticate in IKE_AUTH and create a Child SA with matching traffic selectors.

IKEv2 separates the IKE SA (control) from Child SAs (data). ESP does not negotiate keys by itself.

What changed

  • Emphasis on Left · initiator
  • IKE (UDP/500): Left · initiator → Right · responder
  • Left · initiator: IKE SA → negotiating

How it works

The Security Policy Database (SPD) says what to do with a packet: discard, bypass, or protect. The Security Association Database (SAD) holds the concrete SAs — SPI, algorithms, keys, mode, sequence state. The Security Policy Database (SPD)RFC 4301 · PROPOSED STANDARD · December 2005

ESP provides confidentiality and/or integrity. The SPI and sequence number travel in the clear; the payload and trailer are (when encryption is used) ciphertext, followed by an ICV. Encapsulating Security Payload Packet FormatRFC 4303 · PROPOSED STANDARD · December 2005

AH authenticates without encrypting and covers immutable parts of the outer IP header — which is why NAT breaks AH while ESP with NAT traversal does not. IntroductionRFC 4302 · PROPOSED STANDARD · December 2005

IKEv2 builds an IKE SA, then Child SAs for ESP/AH. Rekey creates a new Child SA before the old one expires when both ends agree on lifetimes and rekey behaviour. RekeyingRFC 7296 · INTERNET STANDARD · October 2014

On the wire

Constructed examples, encoded from the field table below them — not captured traffic.

Cleartext SPI and sequence number before encrypted payload. SPI 0xc0a80101 is an example non-reserved value; sequence 42 is early in the SA lifetime.

Outer IP
In tunnel mode this is a new header to the remote gateway. In transport mode it is the original packet’s IP header with Next Header / Protocol set to 50 (ESP). RFC 4303
ESP header
SPI and Sequence Number — the only ESP fields that are not confidentiality-protected. RFC 4303
Encrypted payload + trailer + ICV
Payload Data (and optional TFC padding), then Padding, Pad Length, and Next Header, covered by encryption when confidentiality is used. An Integrity Check Value follows the ciphertext. RFC 4303

Configure it

IKEv2 + IPsec Virtual-Tunnel Interface toward the peer.

Cisco IOS-XE 17.12 · ISR 4451, Catalyst 8300draft

  1. crypto ikev2 proposal P1
     encryption aes-gcm-256
     prf sha512
     group 14
    crypto ikev2 policy POL
     proposal P1
    crypto ikev2 keyring KR
     peer RIGHT
      address 198.51.100.2
      pre-shared-key <configured out of band>
    crypto ikev2 profile PROF
     match identity remote address 198.51.100.2 255.255.255.255
     authentication local pre-share
     authentication remote pre-share
     keyring local KR

    IKEv2 proposes algorithms and authenticates the peer. The profile binds identity and keyring; it does not by itself define interesting traffic.

    RFC 7296 §1.2

  2. crypto ipsec transform-set TS esp-gcm 256
     mode tunnel
    crypto ipsec profile VTI
     set transform-set TS
     set ikev2-profile PROF
    interface Tunnel1
     ip address 10.255.0.1 255.255.255.252
     tunnel source GigabitEthernet0/0/0
     tunnel mode ipsec ipv4
     tunnel destination 198.51.100.2
     tunnel protection ipsec profile VTI
    ip route 10.2.0.0 255.255.255.0 Tunnel1

    Route-based tunnel: any traffic routed into Tunnel1 is protected. Mode tunnel matches gateway-to-gateway encapsulation.

    Common mistake: Mixing this VTI with a crypto-map ACL that still lists narrow selectors on the peer — the Child SA styles disagree and data stays blackholed.

    RFC 4303 §3.1.2

Verify

show crypto ikev2 sa
IKE SA up to 198.51.100.2.
show crypto ipsec sa
Encaps/decaps counters move together under load.

Caveats

  • Marked draft: IOS-XE IKEv2/VTI syntax varies by release; treat as teaching shape, not copy-paste production.
  • Pre-shared keys shown as placeholders — never commit real secrets.

When it breaks

Symptom first, because that is what you have when it happens.

  1. Phase 1 completes and phase 2 never does, or the tunnel comes up and passes no traffic.

    Narrow it down

    1. Compare the traffic selectors — the interesting-traffic definition — on both ends.
    2. Check whether one side is policy-based and the other route-based.
    3. Read the phase 2 proposal on each end and find the first field that differs.

    Cause

    The selectors do not match. Policy-based IPsec negotiates a security association per selector pair and both ends must agree exactly; route-based negotiates one for any traffic and relies on routing to decide what enters. Mixing them produces a tunnel that establishes and carries nothing.

    Fix

    Make both ends the same style. If one is route-based, configure the other with a selector of any-to-any and let routing choose.

    The Security Policy Database (SPD)RFC 4301 · PROPOSED STANDARD · December 2005
  2. The tunnel works for interactive traffic and fails for file transfers or anything with large packets.

    Narrow it down

    1. Find the largest packet that crosses the tunnel with the do-not-fragment bit set.
    2. Add up the overhead: ESP header, IV, padding, trailer, and the outer IP header.
    3. Check whether ICMP is filtered between the endpoints.

    Cause

    Encapsulation overhead pushes a full-size packet over the path MTU. The tunnel endpoint should signal that, but if ICMP is filtered the sender never learns and simply retransmits.

    Fix

    Clamp the TCP MSS on the tunnel interface. It is the fix that does not depend on ICMP surviving a path you do not control.

  3. A tunnel drops on a schedule — every eight hours, or every hour — and re-establishes on its own.

    Narrow it down

    1. Compare the security association lifetimes configured at each end.
    2. Check whether rekeying is completing before the old association expires.
    3. Look for one end rekeying by volume and the other by time.

    Cause

    Mismatched lifetimes. The shorter one expires first, and if rekeying does not complete cleanly the tunnel drops rather than rolling over.

    Fix

    Match the lifetimes, and ensure both ends support and use the same rekey behaviour. IKEv2 handles this considerably better than IKEv1.

    RekeyingRFC 7296 · INTERNET STANDARD · October 2014
  4. IKE_SA_INIT or Child SA negotiation fails with NO_PROPOSAL_CHOSEN or similar.

    Narrow it down

    1. Compare encryption, integrity, PRF, and Diffie-Hellman groups on both ends.
    2. Check whether one end still offers only IKEv1-era groups the other refuses.
    3. Confirm ESP proposals separately from IKE proposals — they are different negotiations.

    Cause

    The peers have no overlapping cryptographic proposal. Authentication never starts if IKE proposals fail; Child SA failure leaves an IKE SA with no usable data SA.

    Fix

    Publish an overlapping proposal set deliberately. Prefer modern suites both platforms support rather than copying an old checklist from one vendor’s defaults.

    Cryptographic Algorithm NegotiationRFC 7296 · INTERNET STANDARD · October 2014
  5. Outbound encaps counters increment; the peer shows inbound discards or no decrypt.

    Narrow it down

    1. Compare the SPI each side installed for the Child SA.
    2. Confirm the outer destination address matches the SA endpoint.
    3. Look for an old SA still selected after rekey.

    Cause

    The SPI (with destination and protocol) selects the SA. A stale SA, asymmetric install, or traffic hitting the wrong peer address produces ciphertext the receiver cannot map.

    Fix

    Clear and renegotiate Child SAs after config changes; verify both SAD entries list matching SPI pairs and endpoints.

    Security Parameters Index (SPI)RFC 4303 · PROPOSED STANDARD · December 2005

Design notes

Tunnel versus transport is topology, not strength. Gateways use tunnel mode; host-to-host often uses transport. ESP Header LocationRFC 4303 · PROPOSED STANDARD · December 2005

A Child SA that is “up” with idle encaps counters usually means selectors or routing disagree about interesting traffic — not that ESP is broken.

WireGuard solves a narrower problem with static keys and allowed-IPs. Mixing IPsec SPD vocabulary with WireGuard peer lists is how configs get copied wrong.

Misconceptions

IPsec is a protocol.
It is a framework of several: ESP or AH for protecting packets, IKE for negotiating the keys, and a policy database deciding what gets protected. Most confusion comes from a mismatch in one of those three being described as "IPsec is broken". How IPsec WorksRFC 4301 · PROPOSED STANDARD · December 2005
AH and ESP both encrypt.
AH authenticates and does not encrypt at all. ESP can do both, and in practice ESP is used with authentication enabled — which is why AH is nearly extinct, and why it breaks through NAT while ESP with NAT traversal does not. IntroductionRFC 4302 · PROPOSED STANDARD · December 2005
Tunnel mode and transport mode are a security choice.
They are a topology choice. Tunnel mode wraps the whole original packet in a new IP header, which is what lets a gateway protect traffic on behalf of hosts behind it. Transport mode protects the payload of a packet the endpoints send themselves. Both use the same ciphers.

More walkthroughs

Tunnel up, no traffic — selector mismatchfailure

IKE succeeds, but the peers disagree about interesting traffic. Packets miss the SPD or have no matching Child SA, so nothing is protected — or nothing is accepted.

Phase 1 / IKE SA comes up. Operators see “tunnel established.” That only means the control channel and some Child SA exist — not that your prefixes match. A · policy-based: IKE up. B · route-based: IKE up. 10.1.0.0/24. 10.2.0.0/24.

A · policy-basedControl plane healthyIKE: upB · route-basedIKE: up10.1.0.0/2410.2.0.0/24
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
A · policy-basedrouterIKE: up
B · route-basedrouterIKE: up
10.1.0.0/24host
10.2.0.0/24host
A · policy-basedB · route-basedlinkup
10.1.0.0/24A · policy-basedlinkup
10.2.0.0/24B · route-basedlinkup
1 / 4

Phase 1 / IKE SA comes up. Operators see “tunnel established.” That only means the control channel and some Child SA exist — not that your prefixes match.

What changed

  • A · policy-based: IKE → up
  • B · route-based: IKE → up
  • Control plane healthy

Tunnel mode vs transport modecomparison

Tunnel mode wraps the whole original IP packet for gateway-to-gateway protection. Transport mode protects the payload of packets the endpoints send themselves. Same ciphers; different topology.

GW1 encapsulates the entire inner IP packet in ESP and adds a new outer IP header to GW2. Hosts behind the gateways never speak IPsec themselves. GW1 · tunnel mode: Mode tunnel · new outer IP. GW2 · tunnel mode. EP1 · transport. EP2 · transport.

GW1 · tunnel modeProtects traffic for hosts behindMode: tunnel · new outer IPGW2 · tunnel modeEP1 · transportEP2 · transport
  • Link
  • Blocking
  • Packet in flight
  • Discarded
  • Emphasis
Select a device to read its state. Arrow keys walk the topology.
Text equivalent of this diagram
Devices and links at this step
ElementKindState
GW1 · tunnel moderouterMode: tunnel · new outer IP
GW2 · tunnel moderouter
EP1 · transporthost
EP2 · transporthost
GW1 · tunnel modeGW2 · tunnel modelinkup
EP1 · transportEP2 · transportlinkup
1 / 3
tunnel

GW1 encapsulates the entire inner IP packet in ESP and adds a new outer IP header to GW2. Hosts behind the gateways never speak IPsec themselves.

What changed

  • Emphasis on GW1 · tunnel mode
  • outer IP + ESP: GW1 · tunnel mode → GW2 · tunnel mode
  • GW1 · tunnel mode: Mode → tunnel · new outer IP
  • Protects traffic for hosts behind

Terms

SPI
Security Parameters Index: a 32-bit value in the ESP or AH header that, with the destination address and security protocol, selects which Security Association will process the packet.
Traffic selector
The set of addresses, protocols, and ports that a Child SA is allowed to protect. Mismatched selectors are the usual reason an IPsec tunnel is up but carries no traffic.
Tunnel mode
IPsec mode that encrypts (and/or authenticates) an entire inner IP packet and adds a new outer IP header — what gateways use to protect traffic for hosts that are not themselves IPsec peers.

Check yourself

  • A tunnel establishes and carries no traffic. What is the most likely cause?
  • Why does clamping the MSS fix large-packet failures better than relying on path MTU discovery?
  • What does AH provide that ESP does not?
  • A tunnel drops every eight hours and comes straight back. What should you check?
  • What does the Security Policy Database decide?
  • When do you need tunnel mode rather than transport mode?
  • What is the SPI for in an ESP header?
  • What is the relationship between IKEv2 and ESP?