The network layer sets up logical connection for transmitting data from node to node using switching and routing technologies. It also manages error handling, congestion control and packet sequencing. Internet Protocol (IP) is the most popular protocol associated with the network layer, which allows host to host communication. The network layer security ensures that data carried by all IP datagram were encrypted before it is launched into the network and is totally transparent to the underlying application.
IP was formulated in the late 1970s as part of Defense Advanced Research Projects Agency (DARPA) Internet project, in which the network was not large became and all the users were familiar and hence the security was not an issue. As years passed by the Internet becomes global and hence the user’s security concerns also increased. By implementing security at the IP level, an organization can guarantee secure networking for various applications.
Internet Protocol security (IPsec or IP security) is a set of protocols that provides unwavering security for IP with the use of cryptographic techniques. By means of additional headers, it can be incorporated to internet protocol (IPv4 or IPv6). Support for IPsec is optional in IPv4 but mandatory in IPv6. IPsec may be implemented by integrating it with the native IP stack or by implementing an existing implementation of an IP protocol stack or by using a dedicated, inline security protocol processor.
The security protection put forth by IPsec is built on the conditions proposed and maintained by a user or system administrator defined in the Security Policy Database (SPD). Based on the SPD rules, any packet moving through the network is either PROTECTed using IPsec security services, DISCARDed or allowed to BYPASS IPsec protection.
IP security covers the functional areas such as authentication, integrity, confidentiality, replay protection and key management. The authentication mechanism ensures that the source mentioned in the received packet header and the original transmitted source are the same. The integrity mechanism guarantees that in the journey of the packet, it has not been modified. Confidentiality permits communicating nodes to encrypt messages and thereby preventing eavesdrop. The replay protection ensures that a third party cannot seize a datagram and play it back sometime later. The key management facility is concerned with the exchange of keys, used for encrypting/decrypting messages, in a secure manner. In short, secure communications over LAN, private and public WANs and the Internet can be achieved with the help of IPsec.
Regarding the IPsec specification, lot of documents have been published by the IP Security Protocol Working Group set up by the Internet Engineering Task Force (IETF). The most important of these are as follows:
To enhance the security, two protocols such as AH which guarantees the integrity of datagram and ESP, a combination of encryption/authentication protocol are used. Both AH and ESP hold up to two modes of use, namely transport and tunnel mode when used between end-stations and between gateways, respectively.
In the transport mode, ESP encrypts and optionally authenticates the payload of the IP packet but AH authenticates the IP payload and selected portions of the IP header. It offers end-to-end security and having lower overhead than tunnel mode, but needs IPsec to be implemented on the IP storage entities.
In the tunnel mode, the whole IP packet is encrypted and/or authenticated. The IP packet with a new IP header, it is encapsulated into a new IP packet. It is more compatible with existing Virtual Private Network (VPN) gateways and need not have to implement IPsec on the IP storage entity, but has smaller Maximum Transmission Unit (MTU) and more overhead.
An IPsec policy is applied to each IP packet that traverses between the source and the destination, which is decided by the interaction of two databases, namely, the Security Association Database (SAD) and the SPD.
Whether it is using AH or ESP protocol, the sender and the receiver must agree on a key for authentication or encryption algorithm. This set of agreement between the hosts constitutes the Security Association (SA). Security associations are selected on the basis of the security policy. The SA separates the key management and the security mechanisms from each other. An association is a one-way connection, and so for a peer-to-peer communication two SAs are used, one for each direction.
The SA is identified by the following parameters:
The IPsec protocols AH and ESP can be applied to two different versions of IP such as IPv4 and IPv6. Figures 15.1 and 15.2 show the IPv4 and IPv6 datagram format, respectively. IPv4 datagram contains IPv4 Header (6 bits) to specify the transport layer protocol that interprets the data section and IP data which contains TCP fragment.

Figure 15.1 IPv4 datagram format

Figure 15.2 IPv6 datagram format
IPv6 datagram consists of IPv6 header (43 bits), routing extension header (60 bits), destination options header (6 bits) and the IP data. IPv6 header denotes the transport layer protocol. The routing extension header indicates the type of the upper-layer protocol header, namely, AH or ESP, destination options header contains destination information, and IP data which contains TCP fragment.
Authentication Header (AH) protocol which protects all the parts of the IP packet or the datagram with the help of additionally calculated added header which gives authentication mechanism. Header placements and the parts of the datagram for calculation depend upon the IP version and types of modes.
The AH protocol works very simple and does a lot for the network security and it works similar to error detection algorithms like checksum and CRC. The sender uses those algorithms and calculates the checksum of the messages and the result is sent with the contents of the message to the receiver over the network. The receiver computes the checksum and finds if there is any mismatch between the sender and receiver checksums, and will simply discard the message if found. To avoid the use of this well-known common algorithm, some special hashing algorithms and the keys are used by both the parties which are not transparent to the others and a SA is established between these two in calculating and computing the results. A special header is attached for this purpose and the computed result called Integrity Check Value (ICV) is put up and is transmitted in network communication. The receiver on receiving the packet re-computes the ICV and checks for the data integrity and provides authentication but privacy is not guaranteed.
The authentication data field is dynamic to hold varying length of the datagram with the hashing algorithms and the length is a multiple of 32 bits and the header is 32 bits for IPv4 and 64 bits for IPv6. The padding field is included in the authentication data field if necessary. The IPsec AH format contains the following fields and illustrated in Figure 15.3:

Figure 15.3 IPsec authentication header (AH) format
Calculating the AH is same for both IPv4 and IPv6, but differs only in the linking and placing mechanisms of the header.
In IPv6, the AH is added inside the IP datagram and is linked with the next header by placing the header field in the subsequent datagram. This process is performed till the linking transport layer is reached. While in the transport mode, AH is kept inside main IP header preceded to the destination options header and if the ESP header is there it will be placed preceded to that. In the tunnel mode, inside the new IP datagram the AH header appears to be an extended header and does encapsulating the real one to be tunnelled. Figures 15.4 and 15.5 show the AH datagram placement and linking for IPv6 on transport and tunnel mode, respectively.

Figure 15.4 IPv6 AH datagram format – IPsec transport mode

Figure 15.5 IPv6 AH datagram format – IPsec tunnel mode
The same linking technique is followed in IPv4. The datagram carries the protocol field where in the higher layer protocol like TCP/UDP identity is shown by the protocol field and this is the front part of the IP payload and points to the next field. AH places the value into its next header field, places the protocol value inside the IP field and the IP header points to the AH. In transport mode, with the main IP header of the original datagram the AH is added and for the tunnel mode it is added next to the new IP header and it encapsulates the original datagram. The AH datagram placement and linking for IPv4 on transport and tunnel mode is shown in Figures 15.6 and 15.7, respectively.

Figure 15.6 IPv4 AH datagram format – IPsec transport mode

Figure 15.7 IPv4 AH datagram format – IPsec tunnel mode
Authentication header has functional limitations during communication as it protects only the intermediate devices and not the message communication. Therefore, the use of ESP protocol is a boon to the integrity check system. ESP protocol encrypts the IP datagram contents with a key, using an encryption algorithm to maintain integrity and security of the IP datagram and data. The encrypted form of the IP datagram is now repacked and transmitted to the receiver over the network. The receiver will decrypt, to get the data using the same key.
ESP has various fields which are divided into the following three components:
The sequence of these fields indicates a consecutive working mechanism because one field may be the prerequisite for the following fields. This is the main reason of placing ESP header field initially. In the encryption phase, ESP holds the encrypted data and the padding field is used to authenticate the encrypted data. The following are the fields that are included in ESP and it is depicted in Figure 15.8:

Figure 15.8 IPsec Encapsulating Security Payload (ESP) format
The various ESP field calculations and placement are described in the following list:
Figures 15.9 and 15.10 show the placement of ESP fields like ESP header, ESP trailer and ESP authentication data for IPv6 in transport mode and tunnel mode, respectively. The placement of ESP header, ESP trailer and ESP authentication data for IPv4 in transport mode and tunnel mode is shown in Figures 15.11 and 15.12, respectively.

Figure 15.9 IPv6 ESP datagram format – IPsec transport mode

Figure 15.10 IPv6 ESP datagram format – IPsec tunnel mode

Figure 15.11 IPv4 ESP datagram format – IPsec transport mode

Figure 15.12 IPv4 ESP datagram format – IPsec tunnel mode
The application of IPsec varie in the capability of providing a secure communication across a LAN to the Internet.
All types of network are vulnerable to unauthorized access. Security in intranet is an issue due to internal attacks and there are more risks from outside networks as all are interconnected with Internet. So, the password-based access solely cannot protect the data transmitted over the network. The common types of attacks in the computer network which bring forth the need of IPsec are as follows:
Authentication header
Eavesdropping
Encapsulating security payload
ESP header
ESP Trailer
Identity spoofing
IP datagram
IPsec
Routing extension header
Security parameters index (SPI)
Security protocol identifier
Sniffer attack
Transport mode
Tunnel mode