IPSec NAT traversal

Source: Internet
Author: User

References: RFC3715, 3947,3948
 
1. Preface
 
IPSec provides the security of end-to-end IP communication, but there is limited support for IPSec in the NAT environment. AH must not be able to perform NAT, this is contrary to the concept of AH design. In a NAT environment, the ESP Protocol can only have one VPN host to establish a VPN channel, multiple machines cannot communicate with each other in a NAT environment. The IPSec requirements in the NAT environment are described in RFC3715.
 
NAT Traversal (NAT Traversal, NAT-T) is proposed to solve this problem, in RFC3947, 3948 definition, RFC4306 also added NAT-T instructions, but did not abolish RFC3947, 3948, only phase 1 and phase 2 are not differentiated. This method encapsulates the ESP protocol package into the UDP packet (Add a new IP header and UDP header out of the original ESP Protocol IP header ), it can be used in a NAT environment, so that multiple IPSec hosts can establish VPN connections in the NAT Intranet for communication.
 
2. IKE uses UDP encapsulation for negotiation
 
RFC3947 describes how to check whether a NAT device exists and how to negotiate with UDP in IKE to encapsulate IPSec packets.
 
2.1 Detection
 
This function is used to check whether a NAT device and the other party support NAT-T.
 
The source port and destination port of the UDP packet used by regular IKE negotiation are both 500. If a NAT device exists, the source port of the UDP packet will change in most cases. If the receiver finds that the UDP source port is not 500, it can be determined that the data passes through the NAT device. In addition, it is also important to determine the NAT location. When detecting the peer's failure (DPD), we should try our best to take the initiative to conduct DPD detection after the NAT is configured, however, detection from the other side may fail.
 
Check whether the other side supports the NAT-T is achieved by exchanging the vendor ID load, if itself supports NAT-T, in IKE interaction to send this load, the load content is the MD5 Value of "RFC 3947, that is, the hexadecimal "4a1_c8107035845542528f20e95452f ".
 
Determine whether the NAT device is followed by a NAT-D (NAT-Discovery) load, the load content is the HASH value of the IP address and UDP port, the NAT-D load format is as follows, the load type value is 20:
1 2 3 4 5 6 7 8 1 2 3 4 6 7 8 1 2 4 5 6 7 8 1 2 3 4 6 7 8
+ --------------- +
| Next Payload | RESERVED | Payload length |
+ --------------- +
~ HASH of the address and port ~
+ --------------- +
 
The HASH value is calculated as follows. The specific HASH value is determined by negotiation:
HASH = HASH (CKY-I | CKY-R | IP | Port)
The CKY-I and CKY-R are cookies that negotiate the initiator and the responder.
 
In the agreement, both parties should send at least two NAT-D loads, the first load is the peer address and port HASH, the load is their own address and port, if there are multiple local addresses, multiple loads will be sent, including the HASH of all addresses and ports, after receiving the load, the other party calculates the HASH value based on the actual address port of the received packet and then compares it to determine whether there is a NAT device and which side is behind the NAT device.
 
Some NAT devices have a fixed port function, that is, after performing NAT translation, only the address is changed without changing the port. In addition, for IKE communication, cookies are used to distinguish the IKE connections of each internal IPSec device, if IKE negotiation continues with port 500, the problem may occur. For such devices, the solution is to change the IKE negotiation port from 500 to 4500, because these devices only perform special processing on port 500 instead of port 4500. During the negotiation, the party that finds itself after the NAT device immediately wants to change the negotiation port from 500 to 4500, and both the source and destination ports are 4500. At this time, the negotiation packet format is:
Ip udp (4500,4500) <non-ESP marker> HDR *, IDii, [CERT,] SIG_ I
The value of "non-ESP marker" is 4 bytes, which is described later.
 
After receiving the packet decryption and passing the authentication, the receiver needs to change its status to "processing port 500" to "processing port 4500". Port 4500 will be used for subsequent negotiation, in the future, all packets not received by port 500 will be discarded. The negotiation process is as follows:
Initiator Responder
------------------------
UDP (500,500) HDR, SA, VID -->
<-- UDP (500, X) HDR, SA, VID
UDP (500,500) HDR, KE, Ni,
NAT-D, NAT-D -->
<-- UDP (500, X) HDR, KE, Nr,
NAT-D, NAT-D
UDP (4500,4500) HDR * #, IDii,
[CERT,] SIG_ I -->
<-- UDP (4500, Y) HDR * #, IDir,
[CERT,], SIG_R
If the NAT-T is supported, the port value in the ID load should be set to 0.
 
2.2 UDP encapsulation negotiation
 
Two UDP encapsulation methods are available: UDP channel encapsulation and UDP transmission encapsulation. The valid values are as follows:
UDP-Encapsulated-Tunnel 3
UDP-Encapsulated-Transport 4
 
When a NAT device is found, you must select either of the two modes. Generally, the channel mode encapsulation and transmission mode encapsulation values are 1 and 2, respectively. In the negotiation and encapsulation mode, either propose 3 or 4 in NAT mode, or propose 1 or 2 in non-NAT mode, instead of proposing models 1, 2, 3, and 4 at the same time.
 
The IP address is used when calculating the TCP/UDP checksum. Therefore, you need to know the actual original address value of the other party. Therefore, both parties must send their original addresses during negotiation, use the NAT-OA (NAT Original Address) Load for sending, corresponding to the initiator, the Address in the NAT-OA load is the actual Address and the other side of the public Address, and for the responder, the public IP address of the peer in the NAT-OA load and the actual IP address of the owner.
 
The NAT-OA load format is, the type of this load is 21:
1 2 3 4 5 6 7 8 1 2 3 4 6 7 8 1 2 4 5 6 7 8 1 2 3 4 6 7 8
+ --------------- +
| Next Payload | RESERVED | Payload length |
+ --------------- +
| ID Type | RESERVED |
+ --------------- +
| IPv4 (4 octets) or IPv6 address (16 octets) |
+ --------------- +
The ID Type can only be id_rj4_addr or id_1_6_addr, and the reserved field value must be set to 0.
 
It is worth noting that only the NAT-OA load needs to be transferred in the transmission mode, but there is no need to transfer the channel mode, the specific reason can be seen in the next section of the UDP encapsulation method and then explain.
 
The exchange process is as follows:
Initiator Responder
------------------------
HDR *, HASH (1), SA, Ni, [, KE]
[, IDci, IDcr]
[, NAT-OAi, NAT-OAr] -->
<-- HDR *, HASH (2), SA, Nr, [, KE]
[, IDci, IDcr]
[, NAT-OAi, NAT-OAr]
HDR *, HASH (3) -->
 
3. UDP encapsulation of Communication Data
 
RFC3948 describes how to encapsulate and unencapsulate the ESP package through UDP, which is mainly oriented to actual communication data processing.
 
The UDP port used for encapsulating the ESP package is the same as the port negotiated by IKE. Generally, the port is 4500, so that the NAT device does not need to process the two ports, determine whether the IKE negotiation data or the actual ESP data of a UDP encapsulation package is performed based on the SPI field in the ESP header. If the SPI is 0, the IKE data is used; otherwise, the actual ESP data is used.
 
3.1. UDP encapsulated ESP package format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+- +-+
| Source Port | Destination Port |
+- +-+
| Length | Checksum |
+- +-+
| ESP header [RFC4303] |
~ ~
|
+- +-+
 
The defined UDP header is a standard UDP header defined by RFC768. Both the source port and destination port must be the same. The Checksum field should be set to 0. ESP has the integrity authentication function, UDP checksum is not required. The first four bytes of the ESP header are SPI fields, and this field cannot be 0.
 
Therefore, the next TCP upper layer package in transmission mode is encapsulated:
Before applying ESP/UDP
----------------------------
IPv4 | orig IP hdr |
| (Any options) | TCP | Data |
----------------------------
After applying ESP/UDP
-------------------------------------------------------
IPv4 | orig IP hdr | UDP | ESP |
| (Any options) | Hdr | TCP | Data | Trailer | Auth |
-------------------------------------------------------
| <----- Encrypted ----> |
| <------ Authenticated -----> |
 
After the data packet passes through the NAT device, only the data in the external IP address header and UDP header can be modified. The data in the TCP header cannot be modified (the actual NAT device cannot see the TCP header data at all, because it is ESP encrypted), after the ESP/UDP protocol is unencapsulated, the modified external IP address header and original TCP Header are left in the data packets arriving at the application layer, therefore, the checksum in the TCP Header must match the address in the modified IP header, otherwise it seems to be wrong at the application layer, so the IKE must exchange the NAT-OA load, make the ESP/UDP Processing Layer know how to modify the TCP Checksum. For the UDP protocol on the upper layer, you can simply set the checksum to 0.
In channel mode, data is encapsulated:
Before applying ESP/UDP
----------------------------
IPv4 | orig IP hdr |
| (Any options) | TCP | Data |
----------------------------
After applying ESP/UDP
--------------------------------------------------------------
IPv4 | new h. | UDP | ESP | orig IP hdr | ESP |
| (Opts) | Hdr | (any options) | TCP | Data | Trailer | Auth |
--------------------------------------------------------------
| <------------ Encrypted -----------> |
| <------------- Authenticated ------------> |
 
The internal IP header is also part of the ESP load, so the TCP checksum is calculated based on the internal IP header, so the IKE does not need to exchange the NAT-OA load, for unblocking, you only need to unbind UDP and ESP sequentially to restore the original data packets.
 
3.2 UDP encapsulated IKE package format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+- +-+
| Source Port | Destination Port |
+- +-+
| Length | Checksum |
+- +-+
| Non-ESP Marker |
+- +-+
| IKE header [RFC4306] |
~ ~
|
+- +-+
 
The source and destination ports are 4500. The UDP checksum can be calculated or not specified, but this field cannot be used as a basis for determining the package type; the Non-ESP Marker field must be set to 0. The position of this field is equivalent to the SPI field in the ESP header. Therefore, if this field is set to 0, it indicates IKE data. If it is not set to 0, it indicates ESP data.
 
3.3. NAT connection retention package
 
The NAT connection persistence package is used to check whether the NAT ing exists all the time, or if no other package is sent to the other party, this package is used to maintain the connection, but the received package cannot be used as a valid criterion for the connection.
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+- +-+
| Source Port | Destination Port |
+- +-+
| Length | Checksum |
+- +-+
| 0xFF |
+-+
 
The source and destination ports are 4500. The UDP checksum field should be set to 0. The data part is a byte 0xff, and the receiver does not respond.
 
4. Conflict
 
In some cases the NAT-T may cause some conflicts, such as in channel mode, for the following topology:
 
+ ---- + //
| ------------- | ----/
+ ---- + ///
A nat 1/
/
10.1.2.3/
+ ---- + // + ---- ++ ---- +
| ------------- | ---------- + ------ | ---------- |
+ ---- + // + ---- ++ ---- +
B NAT 2 Remote VPN gateway Remote Server
10.1.2.3
 
A and B have the same internal IP address and access the remote server through NAT. However, for the remote VPN gateway and server, the internal IP address displayed after the packet is unwrapped is the same, therefore, if you do not know who to send the data, a conflict will occur. The solution is that the internal IP address of the VPN communication is allocated by the VPN gateway, instead of the local settings, in this way, the VPN gateway can differentiate the access requests of different remote VPN customers. The content of the internal IP address allocation is included in IKEv2 (RFC4306.
 
In transmission mode, problems are more likely to occur. For the following topology:
 
+ ---- +
|
+ ---- +/
A/
10.1.2.3/
/
+ ---- + // + ---- +
| ----- + --------------- |
+ ---- + // + ---- +
B NAT Server
10.1.2.4/
/
/
+ ---- +/
|/
+ ---- +
C
10.1.2.5
 
A and B connect to the server through the VPN channel. If both access the same service on the server, because the transmission mode does not have the original IP header information, therefore, the server package cannot distinguish whether the package is from A or B. Therefore, you cannot determine which SA to use to encrypt the communication. If you add 3rd client C, the server uses plaintext to access the server, the server will be even more blind. The solution still requires communication in channel mode.
 
5. Conclusion
 
NAT-T technology solves the problem of IPSec abnormal use in the NAT environment, although it may also cause some new problems, but it can solve

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.