IPsec: protecting every packet between two points

8 min 5 sections 3 practice questions 8 sources cited

IPsec protects traffic at the network layer, packet by packet, so every program between two points is covered without being changed. It is the usual way to join two offices across the internet or to connect a remote worker to the office network. No background is assumed beyond the earlier network units and everyday ideas of keys and encryption. Questions turn on which of its two protocols, which of its two modes and which key exchange is in use.

By the end of this unit you will be able to:

  • Tell apart AH and ESP by the services each gives and by how each copes with address translation
  • Explain the difference between transport mode and tunnel mode, and what each hides
  • Explain what a security association, an SPI and the security policy database are for
  • Put the steps of IKE in order and say what main, aggressive and quick mode do
  • Choose an IPsec arrangement for host-to-host, site-to-site and remote access

Two surgeries, one patient system, and the internet in between

An illustrative example

A veterinary practice opens a second surgery across town. Both surgeries must use the same patient records system, which runs on a server at the main site. Several programs are involved: the records system, the shared printer, the telephone system and the backup.

The practice manager is offered two quotes. One would add encryption separately to each program. The other would place a small device at each surgery that protects everything passing between the two buildings, whatever program sent it.

Why could one device at each surgery protect every program, without any of the programs being changed?

IPsec protects packets, not programs

Internet Protocol Security (IPsec) is a set of protocols that protect as they travel. It works at , the network layer. Programs above it send ordinary data and never know it is there.

Protection at layer 3 is like a secure courier van between two branches of a firm. Whatever goes into the van, letters, parcels or files, is protected on the journey. Nobody had to wrap each item separately. TLS, by contrast, protects one program's conversation, like a sealed envelope that one sender chooses to use.

  • : the contents are .
  • : any change on the way is detected.
  • Data origin authentication: each packet provably comes from the expected peer.
  • Anti-replay: a captured packet sent again is rejected, because every packet carries a sequence number.
  • : a decides which traffic must be protected.

Integrity and origin are proved with an integrity check value: a short code computed from the packet with a that only the two ends share. A forger without the key cannot produce a matching code.

AH signs the packet; ESP signs and can also encrypt it

IPsec offers two protocols. Each is identified by its own protocol number in the IP header, in the same field that marks as 6 and as 17.

Authentication Header and Encapsulating Security Payload compared
PointAHESP
Full nameEncapsulating Security Payload
IP protocol number5150
Integrity and origin authenticationYesYes
Anti-replayYesYes
Confidentiality, by encryptionNoYes
Covers the outer IP headerYes, every field that does not change in transitNo
Works through NATNoYes, with traversal
Current adviceDiscouraged; optional to implementThe protocol to use

The NAT row follows from the header row. AH's check value covers the source and destination addresses. A NAT device rewrites the source address, so the check fails at the far end and the packet is discarded. ESP's check does not cover the outer addresses, so translation does not break it.

AH: Authentication only, no encryption, breaks at NAT. ESP: Encryption plus authentication. ESP is 50, AH is 51.

Where encryption is not wanted, ESP can run with integrity alone. That is why current guidance treats AH as unnecessary.

Transport mode guards the contents; tunnel mode wraps the packet

Either protocol can be used in one of two modes.

In transport mode, IPsec keeps the original IP header and protects what follows it: the TCP or UDP segment and its data. It is used between two hosts that both run IPsec themselves, such as a server and the backup server it copies to.

In tunnel mode, IPsec wraps the whole original packet, header included, inside a new packet with a new IP header. The new header carries the addresses of the two tunnel ends. is required whenever either end is a security gateway: a or router that protects traffic on behalf of the devices behind it.

What each IPsec mode wraps and what it hides

What each IPsec mode wraps and what it hidesTwo packets side by side, each drawn front to back as three fields. Transport mode: original IP header, ESP header, encrypted segment and data. Tunnel mode: new IP header, ESP header, the whole original packet encrypted.Transport modebetween two hostsOriginal IP headerhosts' real addresses, readableESP headerreadable, marks the start of ESPSegment and dataencrypted by ESPTunnel modea gateway at one end or bothNew IP headertunnel ends' addresses, readableESP headerreadable, marks the start of ESPWhole original packetits IP header too, encrypted by ESPWhat each IPsec mode wraps and what it hidesTwo packets side by side, each drawn front to back as three fields. Transport mode: original IP header, ESP header, encrypted segment and data. Tunnel mode: new IP header, ESP header, the whole original packet encrypted.Transport modebetween two hostsOriginal IP headerhosts' real addresses, readableESP headerreadable, marks the start of ESPSegment and dataencrypted by ESPTunnel modea gateway at one end or bothNew IP headertunnel ends' addresses, readableESP headerreadable, marks the start of ESPWhole original packetits IP header too, encrypted by ESP
Read this figure as text

IPsec's ESP protocol can protect a packet in one of two modes. Both start from the same original packet: an IP header followed by a TCP or UDP segment and its data.

  1. Transport mode, used between two hosts that both run IPsec: the original IP header stays at the front, readable, so anyone on the path sees the two hosts' real addresses.
  2. Next comes the ESP header, which is readable. The segment and its data follow, encrypted by ESP.
  3. Tunnel mode, required whenever either end is a security gateway such as a firewall or router: a new IP header goes at the front, carrying only the addresses of the two tunnel ends.
  4. Next comes the ESP header. Behind it the whole original packet, its own IP header included, is encrypted, so an eavesdropper on the internet sees only that the two gateways are talking.
  5. In both modes ESP also adds a trailer, encrypted with the payload, and an integrity check value at the end, which is not encrypted.
  6. AH in tunnel mode wraps the original packet the same way and signs it, but encrypts nothing, so the inner addresses stay readable. Hiding comes from ESP's encryption, not from the mode.

With ESP in tunnel mode, the original addresses are encrypted along with the data. An eavesdropper on the internet sees only that the two gateways are talking. With AH in tunnel mode the original packet is wrapped and signed but not hidden.

Common misconception

“Tunnel mode always hides the addresses of the devices behind the gateways.”

Hiding comes from encryption, which only ESP provides. AH in tunnel mode leaves the inner addresses readable. Tunnel mode with ESP hides them.

Security associations, and how IKE agrees them

Before two ends can protect anything they must agree the and keys. The agreed set is a security association (SA). An SA covers one direction only, so a two-way conversation needs a pair. Each SA is labelled by a security parameter index (SPI), a number carried in every AH or ESP header so the receiver knows which keys to use.

Each end keeps two lists. The security policy database (SPD) says what to do with each kind of traffic: protect it, let it pass unprotected (bypass), or discard it. The security association database (SAD) holds the keys and settings of every SA in use.

SAs are agreed by the Internet Key Exchange (IKE), over UDP port 500. IKE relies on Diffie-Hellman, a method by which two parties exchange public values and each compute the same secret key, which an eavesdropper cannot work out.

  1. Phase 1: the two ends authenticate each other and build a protected channel for IKE itself. Main mode uses six messages and hides the identities; aggressive mode uses three and does not.
  2. Phase 2: inside that channel, quick mode agrees the SAs that will protect the real traffic, and renews them as they expire.

Those two phases belong to the first version, IKEv1. It was built from the Internet Security Association and Key Management Protocol (ISAKMP), which sets the message framework, and Oakley, which supplies the Diffie-Hellman exchange. IKEv2 replaced it with a simpler start of four messages that sets up both the IKE SA and the first SA for traffic. IKEv2 is preferred, and aggressive mode should be avoided.

Perfect forward secrecy (PFS) means each new key comes from a fresh Diffie-Hellman exchange, never from an older key. A stolen key then exposes only the traffic it protected.

Crossing NAT, L2TP, and the three ways to connect

ESP is not TCP or UDP, so it has no port numbers, and port address translation cannot track it. NAT traversal solves this. IKE notices that a NAT device sits between the ends, and both switch to UDP port 4500 and wrap every ESP packet in a UDP header that the NAT device can translate.

The Layer 2 Tunneling Protocol (L2TP) carries a link-layer connection across an IP network on UDP port 1701. It does not encrypt anything itself. Secured by IPsec ESP, it became a common remote access method, known as L2TP over IPsec. NIST now lists L2TP among older VPN protocols that are no longer suitable.

The three IPsec arrangements
ArrangementJoinsUsual modeExample
Gateway to gateway, or site to siteTwo networks, through a gateway at eachTunnelThe two surgeries
Host to gateway, or remote accessOne device to a network, through its gatewayTunnelA laptop at home reaching the office
Host to hostTwo devices directlyTransportA server and its backup server

A virtual private network (VPN) is any protected link that makes a distant device or network behave as if it were joined directly to another. IPsec is one way to build one.

A question, worked through

A charity's two offices are joined by an IPsec tunnel between gateways. The charity's lawyer says that traffic between the offices must be confidential. The second office's internet provider places all its customers behind port address translation. The network engineer proposes a configuration and the security manager must approve it.

Which configuration BEST meets the requirement?

  1. AH in tunnel mode, with IKEv1 main mode on UDP 500
  2. ESP in transport mode, with IKEv1 in aggressive mode
  3. ESP in tunnel mode, with NAT traversal on UDP 4500
  4. AH and ESP together in tunnel mode, with no NAT traversal
Decide on your answer, then open the reasoning
  1. Three facts decide it: confidentiality is required, both ends are gateways, and a translating device sits on the path.
  2. Rule out a: AH gives no confidentiality, and its check on the addresses fails once they are translated.
  3. Rule out d: the AH part still fails at the translating device, and without NAT traversal the ESP part cannot pass port address translation either.
  4. Between b and c: ESP gives confidentiality in both, but transport mode suits two hosts, not two gateways protecting networks behind them, and aggressive mode exposes the identities.
  5. ESP in tunnel mode encrypts the traffic between gateways, and NAT traversal carries it through translation. The answer is c.

Answer: C. ESP in tunnel mode, with NAT traversal on UDP 4500

Choosing between good answers

A question needs confidentiality from IPsec
the answer includes ESP. AH never encrypts.
IPsec must pass through NAT
choose ESP with NAT traversal on UDP 4500. AH fails at any device that rewrites addresses.
Either end of the protected link is a gateway or firewall
choose tunnel mode. Transport mode is for two hosts that each run IPsec.
A question asks what labels the keys a received packet needs
the answer is the security parameter index, which points to one security association.

Try it

Follow a home worker's request into the office through the IPsec tunnel, and see which addresses each device on the internet can read.

Loading the preview.

Check your understanding

3 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 3

Two hosts that both run IPsec need each packet between them checked for integrity and origin. The traffic must stay readable to an intrusion detection system on the network, and current guidance must be followed.

Which IPsec arrangement meets these needs?

What to remember

  • IPsec protects IP packets at layer 3, so every program between two points is covered without change.
  • AH, protocol 51, gives integrity, origin authentication and anti-replay but no encryption, and breaks at NAT. ESP, protocol 50, adds encryption and is the one to use.
  • Transport mode protects the contents between two hosts. Tunnel mode wraps the whole packet and is used whenever a gateway is an end; with ESP it hides the inner addresses.
  • A security association is one direction of agreed keys, labelled by an SPI. IKE agrees them over UDP 500: phase 1 by main or aggressive mode, phase 2 by quick mode.
  • NAT traversal carries ESP inside UDP 4500. L2TP, on UDP 1701, does not encrypt and relies on IPsec.

Say it from memory

  1. Compare AH and ESP: protocol numbers, services, and behaviour through NAT.

    AH is protocol 51 and gives integrity, origin authentication and anti-replay over the packet and the unchanging parts of its IP header, with no encryption, so it fails through NAT. ESP is protocol 50 and gives the same plus confidentiality; it does not cover the outer header and crosses NAT with NAT traversal on UDP 4500.

    Read this part again

  2. What is the difference between transport mode and tunnel mode, and when is each used?

    Transport mode keeps the original IP header and protects the payload; it is used between two hosts. Tunnel mode wraps the whole original packet in a new one between the tunnel ends; it is used whenever a gateway is an end, and with ESP it hides the inner addresses.

    Read this part again

  3. Name the two phases of IKEv1 and the modes used in each.

    Phase 1 authenticates the peers and builds a protected channel for IKE, using main mode (six messages, identities protected) or aggressive mode (three messages, identities exposed). Phase 2 uses quick mode to agree the security associations for the traffic. IKE runs on UDP 500.

    Read this part again

  4. At which layer does IPsec work, and what five protections can it give?

    IPsec works at layer 3, the network layer, so programs above it never know it is there. It can give confidentiality, integrity, data origin authentication, anti-replay through a sequence number in every packet, and access control through a policy that decides which traffic must be protected.

    Read this part again

  5. How does NAT traversal let ESP cross port address translation, and what are the three IPsec arrangements?

    ESP has no port numbers, so when IKE notices a NAT device both ends switch to UDP port 4500 and wrap every ESP packet in a UDP header. The three arrangements are gateway to gateway, or site to site, and host to gateway, or remote access, both usually in tunnel mode. Host to host usually uses transport mode.

    Read this part again

Sources

  • RFC 4301: Security Architecture for the Internet Protocol, IETFIPsec services: access control, connectionless integrity, data origin authentication, replay detection, confidentiality; SAs are one direction; the SPI identifies an SA; the SPD decides protect, bypass or discard and the SAD holds SA parameters; transport mode between hosts and tunnel mode required when an end is a security gateway.
  • RFC 4302: IP Authentication Header, IETFAH gives integrity, data origin authentication and optional anti-replay, authenticates as much of the IP header as possible, and is protocol number 51.
  • RFC 4303: IP Encapsulating Security Payload (ESP), IETFESP gives confidentiality, data origin authentication, integrity and anti-replay; in tunnel mode it protects the whole inner packet including its header; it is protocol number 50.
  • RFC 3948: UDP Encapsulation of IPsec ESP Packets, IETFESP packets are carried inside UDP to cross NAT, on the same port as IKE, 4500; AH's protection of the outer addresses is inherently incompatible with NAT.
  • RFC 2409: The Internet Key Exchange (IKE), IETFIKEv1 combines ISAKMP with parts of Oakley; phase 1 by main mode, six messages with identities protected, or aggressive mode, three messages without; phase 2 by quick mode; perfect forward secrecy through an extra Diffie-Hellman exchange.
  • RFC 7296: Internet Key Exchange Protocol Version 2 (IKEv2), IETFIKEv2 replaced IKEv1, runs on UDP ports 500 and 4500, and its IKE_SA_INIT and IKE_AUTH exchanges of four messages set up the IKE SA and the first Child SA.
  • NIST SP 800-77 Rev. 1: Guide to IPsec VPNs, NISTGateway-to-gateway, remote access or host-to-gateway, and host-to-host architectures; NIST discourages AH and recommends ESP with null encryption where encryption is not wanted; use IKEv2 and avoid aggressive mode; L2TP carries PPP in UDP 1701, depends on IPsec for protection and is listed among obsoleted and deprecated VPN protocols.
  • RFC 3193: Securing L2TP using IPsec, IETFL2TP relies on IPsec ESP for authentication, integrity, replay protection and confidentiality, with transport mode required, and uses UDP port 1701.