Skip to content
IoT Crest

What is IPsec? Tunnel and transport mode, IKEv2, IoT use

By Hamza Rivera, published , 9 min read

Short answer: IPsec, short for Internet Protocol Security, is a set of IETF standards that protects IP packets with encryption and integrity checks at the network layer, below the applications that send them. In transport mode it protects the payload of a packet between two endpoints. In tunnel mode it protects a whole inner packet inside a new outer header, which suits a gateway that carries traffic for other devices. IKEv2, defined in RFC 7296, authenticates the peers and sets up the keys. TLS and DTLS protect one application connection instead of the whole path, so the choice depends on what a device sends and where it sends it.

What IPsec is, how tunnel and transport mode differ, how IKEv2 sets up the keys, and how IPsec compares with TLS and DTLS for IoT devices.

This article covers what IPsec is, the ESP protocol and its security services, tunnel and transport mode, the IKEv2 key exchange, where IPsec fits for IoT gateways and devices, and how it compares with TLS and DTLS. It does not cover vendor configuration steps, the cipher suites a particular product supports, and the Authentication Header (AH) protocol beyond its place in the architecture.

IPsec, short for Internet Protocol Security, is a set of IETF standards that gives IP traffic confidentiality, integrity and authentication at the network layer, below the applications that send the data. RFC 4301 defines the architecture, RFC 4303 defines the Encapsulating Security Payload (ESP) that carries the protected packets, and RFC 7296 defines the Internet Key Exchange version 2 (IKEv2) that sets up the keys. This guide explains what those parts do, the two packet modes, where IPsec fits for IoT gateways and devices, and how it compares with TLS. For the device side of the picture, read the IoT security basics guide first.

The three parts of IPsec

RFC 4301 says that most IPsec security services come from two traffic security protocols, the Authentication Header (AH) and the Encapsulating Security Payload (ESP), together with cryptographic key management. The table shows the three parts a reader will meet most often.

PartWhat it doesDefined in
ArchitectureSets out the security services, the security policy and the security associations (SAs) that IPsec uses to process trafficRFC 4301
ESPProtects packets with confidentiality, data origin authentication, connectionless integrity and anti-replayRFC 4303
IKEv2Authenticates the two peers and sets up the SAs and their keysRFC 7296

An SA is the building block that ties these together. RFC 4301 defines it as a simplex, meaning one-way, logical connection created for security purposes, where all traffic crossing it gets the same security processing. A two-way exchange therefore uses a pair of SAs, one for each direction.

The SA is only half of the picture. RFC 4301 says an SA is a management construct used to enforce security policy for traffic crossing the IPsec boundary. The policy itself lives in a Security Policy Database (SPD), which specifies what services are to be offered to IP datagrams and in what manner. RFC 4301 also says the SPD is established and maintained by a user or system administrator, or by an application operating within constraints that one of them sets.

ESP is the protocol most people mean when they talk about IPsec traffic. RFC 4303 describes it as an updated version of the protocol, designed to provide a mix of security services in IPv4 and IPv6. The services it lists are confidentiality, data origin authentication, connectionless integrity, an anti-replay service and limited traffic flow confidentiality. The anti-replay service is what RFC 4303 calls a form of partial sequence integrity.

Transport mode and tunnel mode

Each IPsec protocol works in one of two modes. RFC 4301 describes the difference this way: in transport mode, AH and ESP protect mainly the next layer protocols, and in tunnel mode they are applied to tunneled IP packets.

Packet layout: transport mode and tunnel mode Transport mode IP header ESP Payload Trailer, ICV Tunnel mode New IP hdr ESP Orig. IP hdr Payload Trailer, ICV Shaded boxes are protected by ESP. Unshaded boxes are not.
Diagram: an IP packet in transport mode, where ESP sits after the original IP header and protects the payload, and in tunnel mode, where a new outer IP header is added in front of ESP and the whole original IP packet becomes the protected payload.

In transport mode, ESP is inserted after the IP header and before the next layer protocol, such as TCP or UDP. The packet keeps the addresses of its two endpoints. RFC 4301 describes a transport mode security association as typically employed between a pair of hosts to provide end-to-end security. It also allows transport mode between a security gateway and a host, or between two security gateways, when security is wanted between two intermediate systems.

In tunnel mode, the whole original IP packet becomes the protected payload. RFC 4303 describes the inner IP header as carrying the ultimate source and destination addresses, while an outer IP header contains the addresses of the IPsec peers, such as security gateways. Tunnel mode can carry IPv6 over IPv4 and IPv4 over IPv6, because the inner and outer IP versions can differ.

RFC 4301 sets a rule for choosing between them. Whenever either end of a security association is a security gateway, the SA must be tunnel mode, with a small number of exceptions. Two hosts may also agree on a tunnel mode SA between themselves. A device that reaches a site through a gateway therefore sits behind a tunnel mode SA, and a single device talking to a single server can use transport mode.

How IKEv2 sets up the keys

IKE communication always begins with the IKE_SA_INIT and IKE_AUTH exchanges. RFC 7296 describes IKE as a component of IPsec used for performing mutual authentication and establishing and maintaining security associations.

RFC 7296 says that communication using IKE always begins with the IKE_SA_INIT and IKE_AUTH exchanges. It notes that the initial exchanges normally consist of four messages, though in some scenarios that number can grow. Every IKE exchange is a request and response pair.

Setting up the shared state by hand does not scale across many devices, which is the problem IKE solves. RFC 7296 says that establishing this shared state in a manual fashion does not scale well, and that a protocol to establish it dynamically is needed. Version 1 of IKE was defined in RFCs 2407, 2408 and 2409, and RFC 7296 says that IKEv2 replaced all of them.

Where IPsec fits for IoT

The question for an IoT design is where IPsec sits in the path between the device and the service it talks to, and whether the protection should cover the whole link or one application session.

The clearest IoT case for IPsec is a gateway. A gateway that links a site’s local devices to a back-end network is a security gateway in RFC 4301 terms, so the SA between it and the back end normally uses tunnel mode, with the exceptions RFC 4301 lists. In tunnel mode ESP protects the entire inner IP packet, so the devices behind the gateway can send ordinary IP traffic while the gateway applies the protection to it. RFC 4303 says ESP in tunnel mode protects the entire inner IP packet, including the entire inner IP header, so the inner addresses travel inside the protected part of the packet and not in the clear outer header.

The gateway case also shows why the choice of mode matters for addressing. In tunnel mode the outer header carries only the peers’ addresses, such as the two gateways, and the inner addresses belong to the devices and the back-end services. A design that puts the gateways in the path therefore has to plan addressing and routing for both layers.

For a single device sending telemetry to one service, IPsec is one option among several, and it protects more than that device needs. RFC 4301 defines the security policy database that decides which traffic gets which processing, so IPsec protects whatever traffic the policy matches, which can cover many applications on one link. That scope is useful when many services share one link. When only one service is involved, a protocol that works at the application layer matches the scope of the job more closely.

IPsec and TLS on a device

Transport Layer Security (TLS) and its datagram form, Datagram Transport Layer Security (DTLS), protect communication between two applications. TLS version 1.3, defined in RFC 8446, is designed to prevent eavesdropping, tampering and message forgery in communication between client and server applications. Its primary goal is a secure channel between two peers, and the only requirement it places on the transport below is a reliable, in-order data stream.

DTLS version 1.3, defined in RFC 9147, is based on TLS 1.3 and provides equivalent security protection, with the exception of order protection and non-replayability. It preserves the datagram semantics of the underlying transport, which is what makes it the counterpart to TLS for applications that send over UDP. RFC 9147 names media streaming, Internet telephony and online gaming as uses of datagram transports.

The practical difference is scope. IPsec protects traffic between two IP peers, and the security policy decides which traffic it covers. TLS and DTLS protect one connection between two applications, so the device decides what each connection protects. A device that sends data to one cloud endpoint over TLS or DTLS does not have to add IPsec on that same path. A gateway that carries many devices’ traffic to a network is where IPsec’s wider scope fits.

Is IKEv2 still the current key exchange?

Yes. RFC 7296 advances IKEv2 to be an Internet Standard. It obsoletes RFC 5996 and includes the errata for it. Later specifications build on it: RFC 9370 updates RFC 7296 to allow multiple key exchanges to take place while computing a shared secret during a security association setup. RFC 9370 also renames one transform type from Diffie-Hellman Group to Key Exchange Method, and renames a payload field to match, so the naming in RFC 7296 has changed in that respect.

A design that uses IPsec on an IoT link needs three answers. Which end of the SA is a gateway, which decides between tunnel and transport mode under RFC 4301. Which peers authenticate each other, which RFC 7296 covers through IKE_AUTH. And whether the traffic needs protection for the whole link or for one application session, which decides between IPsec and TLS or DTLS. The IoT security basics guide covers the device-side checks that apply whichever protocol is chosen, and the MNO vs MVNO and IoT SIM cards guides cover the network and SIM choices that sit underneath the link.

Sources

Common questions

What is IPsec?

IPsec is a suite of Internet Engineering Task Force (IETF) standards for security services on IP traffic. The architecture is defined in RFC 4301. The Encapsulating Security Payload (ESP), defined in RFC 4303, provides confidentiality, data origin authentication, connectionless integrity, an anti-replay service and limited traffic flow confidentiality.

What is the difference between tunnel mode and transport mode?

In transport mode, ESP sits between the original IP header and the next protocol, such as TCP or UDP, so the packet keeps its original addresses. In tunnel mode, ESP protects the entire inner IP packet, and an outer IP header carries the addresses of the IPsec peers, such as two security gateways. RFC 4301 requires tunnel mode whenever either end of a security association is a security gateway, with a small number of exceptions.

Is IPsec better than TLS for IoT devices?

Neither replaces the other in general. IPsec protects IP traffic between peers, and the security policy decides which traffic that covers. TLS protects a reliable, in-order stream between two applications, and DTLS 1.3 gives equivalent protection over datagram transports such as UDP, with the exception of order protection and non-replayability. A device that sends data to one cloud service can use TLS or DTLS directly, while a gateway that links a whole site to a network may need IPsec.

Is IKEv2 still the current key exchange for IPsec?

Yes. RFC 7296 defines version 2 of the Internet Key Exchange (IKE), obsoletes RFC 5996 and advances IKEv2 to an Internet Standard. RFC 9370 updates RFC 7296 to allow multiple key exchanges during one security association setup.

Written by Hamza Rivera. Facts in this article follow the editorial policy: each one is behavior that is well established or set by the standard named in the text. A confirmed error is fixed in the article and its Updated date changes.

More from IoT security

All IoT security articles

Sponsor an article in this topic

Sponsored articles and in-article link placements for IoT vendors, agencies and SEO buyers. Every sponsored piece is labeled, edited to the same standard as the rest of the site, and placed in the hub its readers already visit. Rates on request.