IPsec

Guide to configure IPsec tunnel

Enterprise Site-to-Site IPsec VPN Lab Guide

Platform: AlmaLinux 10.x / Red Hat Enterprise Linux 10.x (Minimal Installation) Author: Network Specialist Lab Framework: Native Libreswan Implementation


1. Virtualization Hardware & Network Topology

The laboratory environment simulates two geographically separated corporate branches connected via an untrusted public network (The Internet). All infrastructure runs on minimal memory footprints to optimize hypervisor resource utilization.

1.1 VMware Hardware Provisioning

To minimize RAM allocation, configure both Router-A and Router-B virtual machines with the following specifications:

  • Memory (RAM): Allocate 2 GB during the OS installation process (required by the RHEL/AlmaLinux Anaconda installer). Once installation completes, power off the VM and reduce the RAM to 256 MB or 512 MB before production boot.
  • Virtual Network Adapters: Each router must be provisioned with exactly two Network Adapters.

1.2 Virtual Network Segment (LAN Segment) Mapping

To prevent communication leaks into the physical host network, isolate all paths using VMware LAN Segments:

[ BRANCH A INTERNAL LAN ] [ WAN PUBLIC CLOUD ] [ BRANCH B INTERNAL LAN ] VMware LAN Segment: VMware LAN Segment: VMware LAN Segment: “LAN-Segment-A” “WAN-Cloud” “LAN-Segment-B” │ │ │ (Interface: ens224) │ (Interface: ens224) [ ROUTER BRANCH A ] <─────────────── (Interface: ens160) ───────────> [ ROUTER BRANCH B ] 192.168.10.1 203.0.113.10 203.0.113.20 192.168.20.1

1.3 Addressing Blueprint

Device Name OS Interface Assigned IP / Subnet VMware LAN Segment Purpose / Zone
Router-A ens160 203.0.113.10/24 WAN-Cloud Public Internet Facing Endpoint
Router-A ens224 192.168.10.1/24 LAN-Segment-A Branch A Private Default Gateway
Router-B ens160 203.0.113.20/24 WAN-Cloud Public Internet Facing Endpoint
Router-B ens224 192.168.20.1/24 LAN-Segment-B Branch B Private Default Gateway

2. Base Network Configuration & IP Forwarding

Before invoking cryptographic systems, underlying network interfaces must be bound to manual profiles, and routing between interfaces must be permitted inside the Linux kernel.

2.1 Interface Discovery

Query the NetworkManager daemon to discover the active kernel network interface names:

# Display live network hardware device status
nmcli device status

2.2 Router A - Interface and Routing Configuration

Execute the following commands on Router A. If a connection profile does not exist for the secondary internal interface (ens224), it will be explicitly generated:

# Modify the default WAN connection profile to run on a manual static public IP
sudo nmcli connection modify ens160 ipv4.addresses 203.0.113.10/24 ipv4.method manual
sudo nmcli connection up ens160

# Add and bind a brand new connection profile for the LAN physical interface
sudo nmcli connection add type ethernet con-name ens224 ifname ens224 ipv4.addresses 192.168.10.1/24 ipv4.method manual
sudo nmcli connection up ens224

# Enable IPv4 packet forwarding inside the live running Linux kernel
sudo sysctl -w net.ipv4.ip_forward=1

# Persist the IP forwarding configuration across system reboots
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.d/99-ipforward.conf

2.3 Router B - Interface and Routing Configuration

Execute the mirror-image connection profile setup on Router B:

# Modify the WAN connection profile with Router B public parameters
sudo nmcli connection modify ens160 ipv4.addresses 203.0.113.20/24 ipv4.method manual
sudo nmcli connection up ens160

# Add and bind the internal LAN connection profile for Branch B
sudo nmcli connection add type ethernet con-name ens224 ifname ens224 ipv4.addresses 192.168.20.1/24 ipv4.method manual
sudo nmcli connection up ens224

# Enable IPv4 packet forwarding inside the live running Linux kernel
sudo sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.d/99-ipforward.conf

2.4 Basic Connectivity Verification

Verify point-to-point unencrypted WAN infrastructure transit before deploying IPsec layers:

# Execute on Router A to ping the external interface of Router B
ping -c 3 203.0.113.20

3. Deep-Dive Theory: How IPsec IKEv2 Works

A professional deployment requires structural knowledge of the cryptographic mechanisms operating within kernel space. A Site-to-Site VPN using IKEv2 (Internet Key Exchange version 2) separates operations into two modular execution targets:

+------------------------------------------------------------------------+
|                         PHASE 1: IKE SA (Control Plane)                |
|  - Authenticates Router A and Router B (via Pre-Shared Keys)           |
|  - Builds a secure, encrypted management tunnel strictly for daemons   |
|  - Leverages DH Group 14 to compute asymmetric master keys             |
+------------------------------------------------------------------------+
│
▼
+------------------------------------------------------------------------+
|                     PHASE 2: Child SA / IPsec SA (Data Plane)          |
|  - Negotiates data encryption parameters for the actual host workloads |
|  - Evaluates and binds internal subnets via Traffic Selectors          |
|  - Employs ESP to wrap, sign, and encrypt raw production packets       |
+------------------------------------------------------------------------+

3.1 Phase 1 - The IKE Security Association (Control Plane)

The purpose of Phase 1 is to establish a secure management tunnel between public IP endpoints (203.0.113.10 and 203.0.113.20) over UDP port 500. No user data crosses this channel.

  1. Negotiation: Daemons exchange cryptographic policy profiles to agree on symmetric ciphers, integrity hashes, and Diffie-Hellman mathematical groups.
  2. Diffie-Hellman Key Exchange (DH Group 14): An asymmetric key-agreement protocol allowing endpoints to generate a shared secret over an untrusted medium without transmitting the key over the wire. Group 14 (2048-bit MODP) is enforced as the baseline security threshold.
  3. Authentication: Endpoints authenticate identities using an explicit Pre-Shared Key (PSK). Upon successful validation, the permanent control plane channel (IKE SA) is established.

3.2 Phase 2 - The Child SA / IPsec SA (Data Plane)

Operating completely inside the protective shell of the Phase 1 control plane, the daemons negotiate Phase 2 (the Child SA), which handles production data payload encryption.

  1. Traffic Selectors (TS): Structural rules defining which host boundaries are permitted inside the tunnel.
    • Local TS: 192.168.10.0/24
    • Remote TS: 192.168.20.0/24
  2. Encapsulating Security Payload (ESP): The production IP packet is wholly wrapped and encrypted inside an ESP container (IP Protocol 50), blinding the payload to internet adversaries.
  3. Perfect Forward Secrecy (PFS): Forcing an independent Diffie-Hellman key recalculation during Phase 2 ensures that if a long-term master key is compromised in the future, past historical data streams remain unreadable.

3.3 The Cryptographic Suite Selection

Legacy ciphers (3DES, MD5, SHA1, AES-CBC) are vulnerable to side-channel and collision attacks. This guide enforces modern enterprise standards:

  • AES-GCM-256 (Galois/Counter Mode): An Authenticated Encryption with Associated Data (AEAD) block cipher. It performs encryption and integrity authentication simultaneously. Optimized via native CPU instruction sets (AES-NI), it runs exceptionally fast on minimal memory footprints.

4. Native Software Installation

Libreswan is the default, natively supported upstream IPsec implementation inside Red Hat Enterprise Linux and AlmaLinux distributions, relying directly on native kernel sub-systems.

# Install the official Libreswan utility package from native repositories
sudo dnf install -y libreswan

5. Core IPsec Configuration (Libreswan)

Configurations are maintained within isolated profile components in the /etc/ipsec.d/ directory tree.

5.1 Router A Configuration

Create the connection profile file at /etc/ipsec.d/branch-a-to-b.conf on Router A:

# /etc/ipsec.d/branch-a-to-b.conf (Router A Profile)
conn branch-a-to-b
    # Enforce modern IKEv2 protocol syntax exclusively
    keyexchange=ikev2

    # Left Side Parameters (Local Router A Gateway)
    left=203.0.113.10
    leftsubnet=192.168.10.0/24
    leftid=203.0.113.10

    # Right Side Parameters (Remote Router B Gateway)
    right=203.0.113.20
    rightsubnet=192.168.20.0/24
    rightid=203.0.113.20

    # Optimized Cryptographic Cipher Suites
    # Phase 1 (ike): Requires DH group 14 (modp2048) for control plane setup
    ike=aes_gcm256-sha2_256-modp2048
    # Phase 2 (esp): AES-GCM requires no standalone integrity algorithm appended
    esp=aes_gcm256

    # Authentication Framework and Startup Rules
    authby=secret
    auto=start

Create the companion secret credential token storage at /etc/ipsec.d/branch-a-to-b.secrets on Router A:

203.0.113.10 203.0.113.20 : PSK "SuperSecureEnterprisePassword123!"

5.2 Router B Configuration

Create the mirror-image profile file at /etc/ipsec.d/branch-b-to-a.conf on Router B:

# /etc/ipsec.d/branch-b-to-a.conf (Router B Profile)
conn branch-b-to-a
    keyexchange=ikev2

    left=203.0.113.20
    leftsubnet=192.168.20.0/24
    leftid=203.0.113.20

    right=203.0.113.10
    rightsubnet=192.168.10.0/24
    rightid=203.0.113.10

    ike=aes_gcm256-sha2_256-modp2048
    esp=aes_gcm256

    authby=secret
    auto=start

Create the companion secret credential token storage at /etc/ipsec.d/branch-b-to-a.secrets on Router B:

203.0.113.20 203.0.113.10 : PSK "SuperSecureEnterprisePassword123!"

5.3 Activating and Initializing the Tunnel

RHEL 10 deprecates legacy helper scripts (ipsec auto, ipsec update). Configuration and daemon control are handled directly through native systemd interaction.

Execute these commands on BOTH Router A and Router B:

# Enable the background encryption daemon for automatic boot and start it now
sudo systemctl enable ipsec --now

# Force the live service instance to securely read keys from disk into RAM
sudo ipsec secrets

# Initialize the respective connection profile tunnels natively
# On Router A run: sudo ipsec up branch-a-to-b
# On Router B run: sudo ipsec up branch-b-to-a

6. Firewall Hardening & Kernel Tuning (Critical Step)

By default, RHEL security baselines block raw packet transit across dissimilar zones. To prevent admin prohibited filter drops or silent kernel discards, symetrical firewall profiles and adjusted Reverse Path Filtering must be applied on both nodes.

6.1 Symmetrical Firewall Ingress Hardening

Execute these commands on BOTH Router A and Router B to open the public zone facing ens160:

# Permit Internet Key Exchange negotiation traffic
sudo firewall-cmd --zone=public --add-port=500/udp --permanent

# Permit NAT-Traversal encapsulation traffic
sudo firewall-cmd --zone=public --add-port=4500/udp --permanent

# Permit Encapsulating Security Payload network transit protocol (IP 50)
sudo firewall-cmd --zone=public --add-protocol=esp --permanent

6.2 Cross-Zone Policy Routing & Anti-Spoofing Mitigations

Modern firewalld engines require explicit policy definitions to bridge decrypted packet transit between zones. Furthermore, the Linux Reverse Path Filter (rp_filter) must be softened from strict mode (1) to loose mode (2) to prevent the kernel from treating authenticated inbound decrypted LAN traffic as an IP spoof attack.

- Apply on Router A:

# Define the opposing branch subnet domain as an authorized ingress source
sudo firewall-cmd --zone=public --add-source=192.168.20.0/24 --permanent

# Permit standard cross-interface traffic forwarding
sudo firewall-cmd --zone=public --add-forward --permanent
sudo firewall-cmd --reload

# Soften the anti-spoofing mechanism to allow processing of decrypted packets
sudo sysctl -w net.ipv4.conf.all.rp_filter=2

- Apply on Router B:

# Define the opposing branch subnet domain as an authorized ingress source
sudo firewall-cmd --zone=public --add-source=192.168.10.0/24 --permanent

# Permit standard cross-interface traffic forwarding
sudo firewall-cmd --zone=public --add-forward --permanent
sudo firewall-cmd --reload

# Soften the anti-spoofing mechanism to allow processing of decrypted packets
sudo sysctl -w net.ipv4.conf.all.rp_filter=2

7. Enterprise Verification

Verify tunnel health and check security policies directly within kernel space.

7.1 Checking Traffic and Tunnel Counters

# Review live byte counters and established encryption parameters
sudo ipsec whack --trafficstatus

Expected Output: #3: "branch-b-to-a", type=ESP, ..., inBytes=336, outBytes=336, ...

7.2 Verifying Kernel Security Policies (XFRM)

# Inspect the native kernel transforming routing rules
sudo ip xfrm policy

7.3 End-to-End Tunnel Testing

Send an ICMP packet across the encryption matrix sourcing explicitly from the internal network gateways:

# Execute on Router B to verify data plane encryption transit to Router A
ping -I 192.168.20.1 192.168.10.1

Lab Cleanup Guide: Decommissioning Libreswan IPsec VPN

Target Platform: AlmaLinux 10.x / RHEL 10.x Purpose: Restoring network stack and firewall zones to a clean baseline prior to WireGuard testing.

Execute the following sequential blocks to completely purge the cryptographic daemon states, configurations, and restrictive firewall policy changes.


1. Daemon Deactivation & Process Purging

Stop the active cryptographic engine and prevent any automated tunnel triggers upon subsequent reboots. Perform these steps on BOTH Router A and Router B:

# Terminate the running IPsec background processes immediately and flush kernel SAs
sudo systemctl stop ipsec

# Disable the service to ensure the Libreswan daemon does not initialize at boot
sudo systemctl disable ipsec

2. Cryptographic Profile and Secret Eradication

Remove all specific site-to-site configuration profiles and Pre-Shared Keys to clear disk state. Perform on BOTH routers:

# Delete the operational connection profile text structures
sudo rm -f /etc/ipsec.d/branch-*.conf

# Delete the companion Pre-Shared Key credential files
sudo rm -f /etc/ipsec.d/branch-*.secrets

3. Symmetrical Firewall Restorations

Revert ingress rules and block unencrypted intra-zone packet forwarding.

3.1 Device Specific Purging on ROUTER A

# Remove IPsec management ports and ESP protocol allowance from the public zone
sudo firewall-cmd --zone=public --remove-port=500/udp --permanent
sudo firewall-cmd --zone=public --remove-port=4500/udp --permanent
sudo firewall-cmd --zone=public --remove-protocol=esp --permanent

# Revoke authorization for Branch B subnet inside the public zone
sudo firewall-cmd --zone=public --remove-source=192.168.20.0/24 --permanent

# Strip the forwarding capability from the public facing interface zone
sudo firewall-cmd --zone=public --remove-forward --permanent

# Reload the firewalld daemon to apply all changes into running kernel filters
sudo firewall-cmd --reload

3.2 Device Specific Purging on ROUTER B

# Remove IPsec management ports and ESP protocol allowance from the public zone
sudo firewall-cmd --zone=public --remove-port=500/udp --permanent
sudo firewall-cmd --zone=public --remove-port=4500/udp --permanent
sudo firewall-cmd --zone=public --remove-protocol=esp --permanent

# Revoke authorization for Branch A subnet inside the public zone
sudo firewall-cmd --zone=public --remove-source=192.168.10.0/24 --permanent

# Strip the forwarding capability from the public facing interface zone
sudo firewall-cmd --zone=public --remove-forward --permanent

# Reload the firewalld daemon to apply all changes into running kernel filters
sudo firewall-cmd --reload

4. Kernel Parameter Tuning & Interface Cleanup

Re-enable standard anti-spoofing security mitigations and remove simulation interfaces. Perform on BOTH routers:

# Re-enforce strict mode (1) for Reverse Path Filtering to protect against IP spoofing
sudo sysctl -w net.ipv4.conf.all.rp_filter=1
sudo sysctl -w net.ipv4.conf.default.rp_filter=1

# Remove the simulated client dummy interface card (and its bound internal test IPs)
sudo ip link delete dummy0 2>/dev/null

Engineering Note: Do NOT turn off the kernel IP packet forwarding (net.ipv4.ip_forward = 1). Your upcoming WireGuard architecture will require this parameter active to route cross-branch traffic tomorrow.


5. System Reset

Perform a clean reboot on BOTH nodes to ensure a fresh, unpolluted memory space:

# Reboot the operational operating system instance
sudo reboot