ESP
Encapsulating Security Payload, RFC 4303: the IPsec packet that encrypts and authenticates. Decoded since 11.6.0 (#124), with IKE and as part of the defense lot (#125). Only two fields can be read without the SA, the SPI and the sequence number; everything after them (initialization vector, data, padding, next header, ICV) depends on the algorithm IKE negotiated, and stays bytes. The module is packet_parser::parse::transport::protocols::esp, the error type EspError is in packet_parser::errors::transport::esp.
ESP travels two ways, and the crate follows both:
- natively, IP protocol 50 (IPv6 next header 50): the transport layer decodes it into
TransportDetails::Esp, like ICMP, without ports or payload; - in UDP 4500 behind a NAT (RFC 3948): the datagram gets the application label
"ESP", andEspPacket::try_fromdecodes the UDP payload.
Before 11.6.0, protocol 50 already had its name, TransportProtocol::Esp, like every number IANA assigns, and nothing else.
The packet
#![allow(unused)] fn main() { #[non_exhaustive] pub struct EspPacket<'a> { pub spi: u32, // identifies the security association, with the destination address pub sequence: u32, // anti-replay counter, increasing per association pub payload: &'a [u8], // the rest: IV, encrypted data, padding, next header, ICV } }
| Bytes | Field | Note |
|---|---|---|
| 0–3 | spi | big-endian; 0 is reserved and never sent (RFC 4303 §2.1) |
| 4–7 | sequence | big-endian |
| 8 to the end | payload | borrowed; its parts cannot be told apart without the algorithm and the key of the SA |
Two checks, both EspError: fewer than the 8 bytes of SPI and sequence number is PacketTooShort { expected, actual }, an SPI of zero is ReservedSpi. Eight bytes are enough, payload is then empty.
Native ESP: IP protocol 50
The transport dispatch has an arm for ESP, next to the ICMP ones:
#![allow(unused)] fn main() { Some(TransportProtocol::Esp) => Ok(Transport { protocol: TransportProtocol::Esp, source_port: None, destination_port: None, payload: None, details: EspPacket::try_from(payload).ok().map(TransportDetails::Esp), }), }
The consequences are those of ICMP:
payloadstaysNone: nothing readable stacks above ESP, and handing ciphertext to the application probes would only earn it false labels.applicationisNone.- An unreadable packet does not corrupt the flow: the IP header named the protocol, so
protocolisEsp,detailsfalls back toNoneandcorruptedstaysNone.EspPacket::try_from(internet.payload)gives the reason. - The SPI is not part of the identity of the transport layer, which is the protocol and the ports, both
Nonehere:PartialEq,Hashand serialization ignoredetails, andto_owned_flowdrops it. Two ESP packets of different SAs between the same hosts compare equal at the transport layer.
Over IPv6, next header 50 ends the walk of the extension headers (IPv6), and the same arm decodes the packet. Frame 5 of the corpus's site-to-site VPN, the same packet under the SPI of the VPN's other SA, then an IPv6 packet of The-Ultimate-PCAP.pcapng (tested):
#![allow(unused)] fn main() { #[test] fn native_esp_is_decoded_by_the_transport_layer() { let raw = hex::decode(NATIVE_ESP_IPV4_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let transport = flow.transport.as_ref().unwrap(); assert_eq!(transport.protocol, TransportProtocol::Esp); assert_eq!( (transport.source_port, transport.destination_port), (None, None) ); // Nothing readable stacks above: no payload, so no application probe. assert!(transport.payload.is_none()); assert!(flow.application.is_none() && flow.corrupted.is_none()); let Some(TransportDetails::Esp(esp)) = &transport.details else { panic!("expected the ESP header in details"); }; assert_eq!(esp.spi, 0x5fbd_60dc); assert_eq!(esp.sequence, 1); assert_eq!(esp.payload.len(), 112); // IV, ciphertext, trailer and ICV // The JSON names the protocol; the SPI, in details, is serialized // neither with the flow nor with its owned copy. for json in [ serde_json::to_value(&flow).unwrap(), serde_json::to_value(flow.to_owned_flow()).unwrap(), ] { assert_eq!(json["protocol_transport"], "ESP"); let text = json.to_string(); assert!(!text.contains("spi") && !text.contains(&esp.spi.to_string())); } // Nor is it compared: under the SPI of the VPN's other SA, the same // packet has other details but the same transport layer. let mut other_sa = raw.clone(); other_sa[34..38].copy_from_slice(&0xa70b_bb39_u32.to_be_bytes()); let other_flow = parse(LinkType::ETHERNET, &other_sa).unwrap(); assert!(matches!( &other_flow.transport.as_ref().unwrap().details, Some(TransportDetails::Esp(other)) if other.spi != esp.spi )); assert_eq!(other_flow.transport, flow.transport); // Over IPv6, next header 50 ends the extension walk: same layer. let raw = hex::decode(NATIVE_ESP_IPV6_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!(flow.internet.as_ref().unwrap().protocol_name, "IPv6"); let Some(TransportDetails::Esp(esp)) = &flow.transport.as_ref().unwrap().details else { panic!("expected the ESP header in details"); }; assert_eq!((esp.spi, esp.sequence), (0x3d71_3155, 1)); } }
The same frame with its SPI set to zero (tested):
#![allow(unused)] fn main() { #[test] fn an_unreadable_esp_packet_does_not_corrupt_the_flow() { let mut raw = hex::decode(NATIVE_ESP_IPV4_HEX).unwrap(); raw[34..38].copy_from_slice(&[0, 0, 0, 0]); // SPI 0, reserved by RFC 4303 let flow = parse(LinkType::ETHERNET, &raw).unwrap(); let transport = flow.transport.as_ref().unwrap(); assert_eq!(transport.protocol, TransportProtocol::Esp); // still named assert!(transport.details.is_none()); assert!(flow.corrupted.is_none()); // The reason is one call away, on the internet payload. let internet = flow.internet.as_ref().unwrap(); assert_eq!( EspPacket::try_from(internet.payload), Err(EspError::ReservedSpi) ); } }
The corpus has 8 native ESP packets, frames 5 to 12 of ikev2_s2s_ipsec_vpn_aes_gcm.pcapng: two SAs, 0x5fbd60dc and 0xa70bbb39, sequence numbers 1 to 4 each. The-Ultimate-PCAP.pcapng has 1 593, 542 over IPv4 and 1 051 over IPv6, the counts tshark gives; a run of 11.6.0 decodes every one of them into TransportDetails::Esp.
In UDP 4500, behind a NAT
A NAT rewrites ports, and ESP has none. Behind a NAT, ESP rides in UDP 4500 (RFC 3948), sharing the port with IKE and with the keepalives that hold the NAT mapping open. The IKE page explains how UdpEncapsulation::classify tells the three apart; two rules of the table, right after the IKE rule, give the other two labels:
#![allow(unused)] fn main() { port_rule(ApplicationProtocol::Esp, Guard::Udp, is_nat_t_udp_port, ProbeId::EspNatT), // 4500 port_rule(ApplicationProtocol::NatKeepalive, Guard::Udp, is_nat_t_udp_port, ProbeId::NatKeepalive), // 4500 }
Both look at UDP 4500 only, on either side; UDP 500 carries neither.
"ESP":classifyreturnsUdpEncapsulation::Esp, at least 8 bytes that do not start with the four zero bytes of the non-ESP marker, andEspPacket::try_fromaccepts them. The flow keeps its UDP transport, and the ESP packet istransport.payload, decoded byEspPacket::try_fromwhen needed. It is a label, not a tunnel: what ESP carries is encrypted, andinnerstaysNone."NAT-keepalive": a single0xFFbyte (RFC 3948 §2.3),UdpEncapsulation::Keepalive. There is nothing to decode. The corpus ofprotocols/ipsec/has none;The-Ultimate-PCAP.pcapnghas three, frames 46989, 47000 and 47009.
Frame 11 of ipsec_isakmp_esp.pcap, then the first keepalive of the Ultimate PCAP (tested):
#![allow(unused)] fn main() { #[test] fn esp_and_keepalives_on_udp_4500() { // Frame 11: an ESP packet, non-zero SPI first. let raw = hex::decode(NAT_T_ESP_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!( flow.application.as_ref().unwrap().protocol.label(), "ESP" ); assert!(flow.inner.is_none()); // a label, not a tunnel: nothing to peel let payload = flow.transport.as_ref().unwrap().payload.unwrap(); assert_eq!( UdpEncapsulation::classify(payload), Ok(UdpEncapsulation::Esp(payload)) ); let esp = EspPacket::try_from(payload).unwrap(); assert_eq!((esp.spi, esp.sequence), (0xcbb7_a0c6, 1)); assert_eq!(esp.payload.len(), payload.len() - 8); // Frame 46989 of the Ultimate PCAP: one byte, 0xFF. let raw = hex::decode(NAT_KEEPALIVE_HEX).unwrap(); let flow = parse(LinkType::ETHERNET, &raw).unwrap(); assert_eq!( flow.application.as_ref().unwrap().protocol.label(), "NAT-keepalive" ); let payload = flow.transport.as_ref().unwrap().payload.unwrap(); assert_eq!(payload, [0xff]); // the Ethernet trailer is cut by IPv4 assert_eq!( UdpEncapsulation::classify(payload), Ok(UdpEncapsulation::Keepalive) ); } }
ESP in UDP on another port is declared with ParseConfig::decode_as(port, DecodeAsProtocol::Esp), a variant added in 11.6.0: the same probe and the same transport guard as the rule of port 4500. The keepalive has no DecodeAsProtocol (tested):
#![allow(unused)] fn main() { #[test] fn esp_on_another_port_needs_decode_as() { let mut raw = hex::decode(NAT_T_ESP_HEX).unwrap(); move_to_port_4501(&mut raw); assert_eq!(label(&raw, &ParseConfig::new()), Some("Unknown")); let config = ParseConfig::new().decode_as(4501, DecodeAsProtocol::Esp); assert_eq!(label(&raw, &config), Some("ESP")); // A declaration names a protocol, and that protocol's probe decides. let config = ParseConfig::new().decode_as(4501, DecodeAsProtocol::Ike); assert_eq!(label(&raw, &config), Some("Unknown")); // The keepalive has no Decode As: off 4500, its byte is just a byte. let mut raw = hex::decode(NAT_KEEPALIVE_HEX).unwrap(); move_to_port_4501(&mut raw); let config = ParseConfig::new().decode_as(4501, DecodeAsProtocol::Esp); assert_eq!(label(&raw, &config), Some("Unknown")); } }
Oracle
The golden test of the IKE page, tests/ipsec_golden.rs (oracle), covers ESP too: 323 packets, SPI and sequence number compared with tshark 4.6.6, the 8 native ones and the 315 datagrams of ipsec_isakmp_esp.pcap, in UDP 4500. For a native packet it also checks that the transport has no payload and the flow no application; for a datagram, that the label is "ESP" and that classify returns Esp. In the golden histogram "ESP" went from 0 to 315, and the native packets stay without application, like ICMP. The fuzz target parse_ipsec runs EspPacket::try_from nightly: when it accepts, the SPI is not zero and payload is everything after the first 8 bytes.
The Ultimate PCAP, which the golden test does not read, adds 7 datagrams labelled "ESP" and the 3 keepalives above.
What is left out
The limits every protocol shares, reassembly and checksums among them, are on the Limits page. The ones below are this protocol's own.
- Nothing is decrypted, and the end of the packet is not split: padding, next header and ICV cannot be told apart without the key and the algorithm of the SA, which a stateless parser does not have.
- No SA state:
sequenceis read, not checked against an anti-replay window, and no ESP SPI is linked to the IKE exchange that negotiated it. - AH (IP protocol 51) is not decoded: over IPv4 the flow gets
TransportProtocol::Ahwithout details; over IPv6, AH is one of the extension headers the internet layer walks, and what follows it is the transport protocol. - ESP over TCP is not recognized: both rules are
Guard::Udp.