IPsec ESP Transport vs Tunnel Mode, and NAT-T, Packet by Packet
This walkthrough uses five captures from the late Nick Russo's free packet-capture library. They are in the IPsec and MACsec archive, ipsec_macsec_pcap.zip, under ipsec_macsec_pcap/direct_encap/: espnull_direct_transport.pcapng, espnull_direct_tunnel.pcapng, espnull_direct_transport_natt.pcapng, espnull_direct_tunnel_natt.pcapng, and espaes_direct_tunnel.pcapng. The archive is preserved by the Internet Archive, with the rest of the library listed on his job aid page. We decoded the captures with VisualEther, which turns a Wireshark PCAP into a sequence diagram with a plain-English caption on every message.
IPsec protects IP packets between two devices, most often two VPN gateways. Its data-carrying protocol, ESP (Encapsulating Security Payload), normally encrypts everything after its own header, so a capture of a working VPN shows little more than addresses and a counter.
These captures are different. Four of them use ESP-NULL: ESP with integrity protection but no encryption (RFC 2410). Every packet is still authenticated, yet Wireshark can show what ESP carries. That makes the differences between IPsec's modes visible, field by field:
- Transport mode: ESP protects a router's own ping.
- Tunnel mode: ESP carries a whole packet from one network to another, inside a second IP header.
- Transport mode through a NAT: IKE detects the NAT and moves everything to UDP port 4500.
- Tunnel mode through a NAT: the most deeply nested packet, four layers.
- Tunnel mode with AES: the same packet as capture 2, encrypted, for contrast.
Each capture is six frames: an IKEv2 exchange that sets up the keys, then one ping and its reply. Each is drawn as an annotated sequence diagram, in three forms: an interactive viewer, an HTML page, and a printable PDF. In the viewer and the HTML page, clicking any message opens its full Wireshark field tree.
💡 Read the walkthrough and a diagram side by side: open the interactive diagram in a split-screen window, or open the HTML version in a new tab and snap the two windows side by side. Each section below links its own diagram.
IPsec in one minute
IPsec has two jobs, done by two protocols:
- IKE (Internet Key Exchange) agrees on keys. The two peers authenticate each other and agree on algorithms and keys. These captures use IKEv2 (RFC 7296) on UDP port 500.
- ESP protects the data. Each protected packet carries an ESP header, the protected payload, an ESP trailer, and an integrity check (RFC 4303). ESP is IP protocol 50, not a TCP or UDP service.
The agreement IKE produces is a security association (SA): a one-way set of keys and algorithms. Data needs two, one for each direction, and each is named by a 32-bit SPI (Security Parameter Index) that the receiving side picks. Every ESP packet starts with the SPI, so the receiver knows which keys to use.
ESP works in two modes. Transport mode protects the payload of a packet and keeps its original IP header. Tunnel mode protects the entire original packet and puts a new IP header, between the two gateways, in front of it.
The captures come from one lab. R2 and R4 are the IPsec peers in captures 1, 2, and 5, with R3 routing between them. R6 sits behind R3, which translates R6's private address for the outside. All captures were taken on R3's links: between R2 and R3, or between R6 and R3.
What happens before the first ESP packet?
IKEv2 runs two exchanges, four messages in all, and every capture starts with them. The first pair, IKE_SA_INIT, agrees on algorithms and runs a Diffie-Hellman exchange. The second, IKE_AUTH, is already encrypted. It authenticates the peers and sets up the first pair of ESP SAs, called a Child SA.
All five captures propose the same suite for the IKE SA: AES-CBC with a 256-bit key, HMAC-SHA2-384 for the PRF (pseudo-random function) and for integrity, and Diffie-Hellman group 20, a 384-bit elliptic curve. The responder accepts it, and the whole negotiation takes 15 to 44 ms.
IKE_AUTH carries one Encrypted and Authenticated payload, so a capture can't show the identities, the Child SA's algorithms, its traffic selectors, or the mode requested. The ESP packets that follow show the result instead. For the IKEv2 messages themselves, see our IKEv2 IPsec VPN sequence diagram.
The first message also carries two notifies, NAT_DETECTION_SOURCE_IP and NAT_DETECTION_DESTINATION_IP. They matter in captures 3 and 4.
Capture 1: transport mode
R2 pings R4, and the two routers protect the ping themselves. The routers are both the IPsec peers and the endpoints of the traffic, which is when transport mode applies.
What does an ESP packet look like?
Frame 5 is the ping. Its IP header is the ping's own, from 10.2.3.2 to 10.3.4.4, except that the protocol field says ESP (50). Then come the ESP fields:
| Part | Field | Frame 5 | Purpose |
|---|---|---|---|
| Header | SPI | 0xa434fb0b | Which SA, so which keys, the receiver should use |
| Header | Sequence Number | 1 | Counts packets on the SA, so the receiver can reject replays |
| Payload | ICMP echo request | The protected data, readable here because the cipher is NULL | |
| Trailer | Padding | 01 02 | Aligns the trailer to a 4-byte boundary |
| Trailer | Pad Length | 2 | How many padding bytes to strip |
| Trailer | Next Header | ICMP (1) | What the payload is |
| ICV | 12 bytes | Integrity Check Value over the header, payload, and trailer |
The Next Header field is in the trailer, after the payload, so with real encryption it is hidden too. Here it says ICMP: the payload is the ping message itself, with no second IP header. That is transport mode.
The 100-byte ping becomes a 124-byte packet: 8 bytes of ESP header, 4 of padding and trailer, and 12 of ICV.
Why does the reply have a different SPI?
Because it travels on the other SA of the pair. R4's reply in frame 6 carries SPI 0xd2cb426b, the SPI R2 chose for traffic it receives, and its sequence number is 1 again, because each SA keeps its own counter.
Capture 2: tunnel mode
Now R2 and R4 act as VPN gateways. R1, behind R2, pings R5, behind R4. Neither host runs IPsec; the gateways protect the traffic on their behalf.
What is the difference between transport and tunnel mode?
Tunnel mode adds a second IP header. Frame 5 has an outer header from 10.2.3.2 to 10.3.4.4, gateway to gateway, then ESP, then the original packet from 10.1.2.1 to 10.4.5.5, intact. ESP's Next Header is IPIP (4), meaning the payload is a whole IP packet. In capture 1 it was ICMP (1).
Here is frame 5's full decode. Open the layers in order: outer IP, ESP with its trailer fields, inner IP, and ICMP.
frame : Frame 5: Packet, 158 bytes on wire (1264 bits), 158 bytes captured (1264 bits) on interface -, id 0
Interface id : 0 (-)
eth : Ethernet II, Src: aa:bb:cc:00:02:10, Dst: aa:bb:cc:00:03:10
Destination : aa:bb:cc:00:03:10
Source : aa:bb:cc:00:02:10
ip : Internet Protocol Version 4, Src: 10.2.3.2, Dst: 10.3.4.4
Differentiated Services Field : 0x00 (DSCP: CS0, ECN: Not-ECT)
000. .... = Flags : 0x0
esp : Encapsulating Security Payload
ESP ICV : da4662abe3c03998e91d958a [unchecked]
ip : Internet Protocol Version 4, Src: 10.1.2.1, Dst: 10.4.5.5
Differentiated Services Field : 0x00 (DSCP: CS0, ECN: Not-ECT)
000. .... = Flags : 0x0
icmp : Internet Control Message Protocol
data
| Transport mode (capture 1) | Tunnel mode (capture 2) | |
|---|---|---|
| Protects | The payload of the packet | The whole original packet |
| IP headers | One, the original | Two: a new outer one, and the original inside ESP |
Next Header | The payload protocol, here ICMP (1) | IPIP (4) |
| Who the peers are | The traffic's own endpoints | Gateways for whole networks |
| Size of the 100-byte ping | 124 bytes | 144 bytes |
Routers between the gateways see only the outer header. With encryption, as in capture 5, an observer can't even tell which hosts are talking. Site-to-site VPNs use tunnel mode for this reason, and because the inner addresses can be private ranges that the network between the gateways can't route.
The TTLs show who routed what. The inner request has TTL 254: R1 sent it with 255, and R2 decremented it when routing it into the tunnel. The outer header has TTL 255, set by R2 itself.
Why is there a 2-second gap after IKE?
The ping arrives 2 seconds after IKE_AUTH, and it is sequence number 1, not 0. Cisco's ping starts at 0 and waits 2 seconds for each reply, so the likely story is that echo 0 reached R2 with no tunnel up, started the IKE negotiation, and was dropped. The capture, taken between R2 and R3, can't show the dropped echo, so this is a reading of the evidence rather than proof.
Capture 3: transport mode through a NAT
R6 has a private address, 192.168.0.6. R3 translates it to 10.3.4.6 for the outside, so R4 believes it is talking to 10.3.4.6. The capture was taken on R6's side of the NAT, so every frame shows the private address.
How does IKEv2 detect a NAT?
With the two NAT detection notifies in IKE_SA_INIT. Each holds a SHA-1 hash of the IKE SPIs, an IP address, and a port (RFC 7296 section 2.23). NAT_DETECTION_SOURCE_IP hashes the address and port the sender thinks it is sending from, and NAT_DETECTION_DESTINATION_IP the ones it is sending to. The receiver hashes the addresses it actually sees and compares.
The hashes can't be reversed, but the candidate addresses are few, so we hashed them to see what each side put in:
| Frame | Sender | Source hash covers | Destination hash covers |
|---|---|---|---|
| 1 | R6 | 192.168.0.6, port 500 | 10.3.4.4, port 500 |
| 2 | R4 | 10.3.4.4, port 500 | 10.3.4.6, port 500 |
R4's destination hash shows it addressed its reply to 10.3.4.6. That hash is our only source for the translated address; it appears in no frame. Each side now finds a mismatch:
- R6 receives a destination hash for
10.3.4.6, not its own192.168.0.6, so it is behind a NAT. - R4 receives a source hash for
192.168.0.6, but the packet came from10.3.4.6, so its peer is behind a NAT.
In capture 1, by contrast, every hash matches the real addresses, and IKE stays on port 500.
Why does IPsec switch to UDP port 4500?
Because ESP has no ports, and a NAT needs them. A NAT that shares one public address among many hosts tells their traffic apart by port number. ESP is a bare IP protocol, so many NATs drop it or can track only one host's. NAT traversal (NAT-T) gives ESP a UDP header the NAT can translate (RFC 3948).
Once a NAT is detected, the initiator moves IKE to UDP port 4500 (RFC 7296 section 2.23), as frame 3 shows. Port 4500 now carries both IKE and ESP, so each IKE message starts with a 4-byte Non-ESP Marker of all zeros. An ESP SPI is never zero, so the receiver can tell the two apart (RFC 3948 section 2.2).
Frame 5 shows the result: IP, then UDP 4500 to 4500, then the ESP header, then the ping, still in transport mode. The UDP checksum is zero, as RFC 3948 section 2.1 recommends; ESP's ICV already protects the contents. The UDP header adds 8 bytes, making the packet 132 bytes.
What breaks in transport mode behind a NAT?
TCP and UDP checksums include the source and destination IP addresses, and the NAT changed them. In transport mode the original header is the only header, so after decryption the receiver must correct those checksums using the original addresses, which IKEv2 exchanges in the traffic selectors (RFC 3948 section 3.1.2, RFC 7296 section 2.23.1). An ICMP checksum doesn't cover the addresses, so this ping needs no correction.
Capture 4: tunnel mode through a NAT
This time R4, outside the NAT, starts the negotiation, sending to R6's public address 10.3.4.6. That works only because R3 has a static translation for R6. With dynamic NAT, no mapping would exist until R6 sent something first.
The NAT detection plays out as in capture 3, from the other side. R4's destination hash covers 10.3.4.6, which tells R6 it is behind a NAT. R6's source hash covers 192.168.0.6, which tells R4 the same about its peer. IKE_AUTH moves to port 4500.
What does a tunnel-mode packet look like through a NAT?
R5, behind R4, pings 10.6.6.6, a loopback address on R6. Frame 5 is the most deeply nested packet in these captures:
- Outer IP,
10.3.4.4to192.168.0.6. R4 sent it to10.3.4.6; R3 rewrote the destination. - UDP, port 4500 to 4500, checksum zero.
- ESP, SPI
0xe09b01d1,Next HeaderIPIP (4). - Inner IP,
10.4.5.5to10.6.6.6, carrying the ICMP echo request.
The 100-byte ping becomes 152 bytes: 20 for the outer IP header, 8 for UDP, 8 for the ESP header, 4 for padding and trailer, and 12 for the ICV.
The NAT rewrites only the outer header. The inner packet, and its checksums, arrive untouched, so tunnel mode needs none of the checksum correction that capture 3 does.
The reply in frame 6 comes from 10.6.6.6 with an inner TTL of 255. R6 owns that address, so it builds the reply and puts it straight into the tunnel, without routing it first.
Capture 5: the same tunnel, encrypted with AES
The same gateways, R2 and R4, and the same IKE exchange as capture 2. This time the Child SA encrypts.
What can you see in an encrypted ESP packet?
Only the outer IP header, the SPI, and the sequence number. Compare this decode of frame 5 with the one from capture 2 above:
frame : Frame 5: Packet, 182 bytes on wire (1456 bits), 182 bytes captured (1456 bits) on interface -, id 0
Interface id : 0 (-)
eth : Ethernet II, Src: aa:bb:cc:00:02:10, Dst: aa:bb:cc:00:03:10
Destination : aa:bb:cc:00:03:10
Source : aa:bb:cc:00:02:10
ip : Internet Protocol Version 4, Src: 10.2.3.2, Dst: 10.3.4.4
Differentiated Services Field : 0x00 (DSCP: CS0, ECN: Not-ECT)
000. .... = Flags : 0x0
esp : Encapsulating Security Payload
Wireshark stops after ESP Sequence. The inner IP header, the ICMP message, the padding, and even the Next Header field are ciphertext. An observer can't tell whether this is tunnel or transport mode, or what protocol is inside.
The size still gives the cipher away. The packet is 168 bytes, 24 more than the 144 bytes in capture 2. That fits AES in CBC mode exactly:
- A 16-byte initialization vector goes before the ciphertext (RFC 3602).
- AES encrypts in 16-byte blocks, so the 100-byte inner packet and the 2 trailer bytes are padded to 112 bytes: 8 more padding bytes than the 4-byte alignment of ESP-NULL needs.
The Child SA's algorithms were negotiated inside the encrypted IKE_AUTH, so AES-CBC with a 12-byte ICV is an inference from the sizes, not something the capture states.
Comparing the five captures
| Capture | Mode | Outer layers | Next Header | IP packet for a 100-byte ping | Overhead |
|---|---|---|---|---|---|
| 1 | Transport | IP | ICMP (1) | 124 bytes | 24 |
| 2 | Tunnel | IP | IPIP (4) | 144 bytes | 44 |
| 3 | Transport, NAT-T | IP, UDP 4500 | ICMP (1) | 132 bytes | 32 |
| 4 | Tunnel, NAT-T | IP, UDP 4500 | IPIP (4) | 152 bytes | 52 |
| 5 | Tunnel, AES | IP | Encrypted | 168 bytes | 68 |
Every capture shares the same 24 bytes of ESP-NULL overhead: an 8-byte header, 4 bytes of padding and trailer, and a 12-byte ICV. Tunnel mode adds 20 for the outer IP header, NAT-T adds 8 for UDP, and AES-CBC adds 24 for the IV and block padding. On a link with a 1500-byte MTU, that overhead is why VPN tunnels often lower the TCP MSS (maximum segment size) or the tunnel MTU.
Why use ESP-NULL at all?
RFC 2410 defines NULL encryption so that ESP can provide integrity and replay protection without confidentiality. Each packet is still authenticated by its ICV and counted by its sequence number. It suits traffic that must not be tampered with but needn't be secret, or networks where monitoring tools must see inside the packets.
A receiver knows an SA uses NULL encryption because IKE said so. Wireshark doesn't have the IKE keys, so it can't know, and by default it shows ESP-NULL packets as opaque, just like AES. Its esp.enable_null_encryption_decode_heuristic preference tells it to try reading the payload as clear text. Turn it on only for captures you know use ESP-NULL. These four decode cleanly with it: valid Next Header values, padding bytes counting 01 02 as RFC 4303 specifies, and correct ICMP checksums.
Frequently asked questions
What is the difference between IPsec transport mode and tunnel mode? Transport mode keeps the original IP header and protects only what follows it. Tunnel mode protects the whole original packet and adds a new outer IP header between the two gateways. In a capture, ESP's Next Header shows the difference: the payload protocol (ICMP here) for transport mode, IPIP (4) for tunnel mode.
Which ports does IPsec use? IKE uses UDP port 500, and UDP port 4500 once a NAT is detected. ESP itself is IP protocol 50 with no ports, unless NAT-T wraps it in UDP port 4500.
What is NAT-T in IPsec? NAT traversal: when IKE detects a NAT between the peers, both IKE and ESP move to UDP port 4500, so the NAT has ports to translate. Captures 3 and 4 show it.
How does IKEv2 know there is a NAT? Each side sends hashes of the addresses and ports it thinks it is using. If the hash doesn't match what the other side sees, an address was translated on the way.
Is ESP-NULL secure? It protects integrity, not confidentiality. Packets can't be altered or replayed undetected, but anyone on the path can read them, as these captures show.
Takeaways
- IKE sets up the keys; ESP carries the data. IKEv2 needs four messages, and the last two are already encrypted.
Next Headertells transport from tunnel mode. ICMP (or TCP, UDP) means transport mode;IPIP (4)means a whole packet is inside.- Each direction is a separate SA with its own SPI and its own sequence counter.
- NAT detection works by hashing addresses. A mismatch moves IKE and ESP to UDP port 4500, with a Non-ESP Marker to tell IKE from ESP.
- Tunnel mode passes through NAT more cleanly, because the NAT never touches the inner packet's checksums.
- Encrypted ESP still leaks sizes. The 24 extra bytes in capture 5 point to AES-CBC, even though the cipher was negotiated in secret.