Configuring Site-to-Site IPsec VPN with strongSwan on CentOS Stream 9: Lessons from a Midnight Outage

2:15 AM: A Ringing Phone and a Devastating Tunnel Crash

At exactly 2:15 AM, my phone buzzed violently. On the other end, the shift lead’s voice sounded frantic: all warehouse inventory data from the Da Nang branch could not be pushed to the central ERP cluster at the Hanoi headquarters. Pings between the two internal gateways showed 100% packet loss. Application logs were flooded with ETIMEDOUT error codes. Less than four hours remained before over 200 morning-shift workers would arrive to scan outbound dispatch labels.

The link connecting both sites ran over a Site-to-Site IPsec tunnel powered by strongSwan. Previously, the infrastructure had run smoothly on CentOS 7. Just last week, the team deployed a pilot gateway in Da Nang running CentOS Stream 9 (kernel 5.14). In the middle of the night, an automated cron job ran dnf upgrade, pulled down a fresh kernel, and rebooted the system. The result: the IKEv2 tunnel collapsed entirely, and the service failed to recover on its own. Rushing to open my laptop, I SSH’d directly into the gateway and began inspecting each network layer.

The Core Mechanics of Site-to-Site IPsec: Stop Blindly Guessing Configurations

Without grasping the underlying mechanisms, it is easy to fire off random commands and accidentally break your corporate routing table. With IPsec, packets follow a strictly defined lifecycle:

  • IKEv2 (Phase 1): The secure handshake session between both gateways. Both ends authenticate using a Pre-Shared Key (PSK) or digital certificates, then perform a Diffie-Hellman key exchange (e.g., MODP 2048-bit group) to encrypt the control channel.
  • IPsec Child SA (Phase 2): The actual data transmission channel. All traffic traveling between the two internal LAN subnets is encapsulated in an ESP (Encapsulating Security Payload, IP Protocol 50) header and encrypted with AES-256.
  • swanctl Replaces ipsec.conf: On CentOS Stream 9, the swanctl utility along with the modern syntax in swanctl.conf completely supersedes the legacy ipsec.conf/ipsec.secrets format, providing significantly greater connection management flexibility.
  • XFRM Subsystem in the Linux Kernel: LAN packets reaching the gateway do not follow traditional routing tables. The kernel captures packets using XFRM (Transform Policy), encapsulates them into ESP, and only then forwards them out via the WAN interface.

The network topology deployed in production:

  • Hanoi Gateway (Site A): WAN IP 203.0.113.10 — Internal LAN 192.168.10.0/24
  • Da Nang Gateway (Site B): WAN IP 198.51.100.20 — Internal LAN 192.168.20.0/24

Step-by-Step Tunnel Deployment and Permanent Troubleshooting

1. Install strongSwan and Verify Kernel Modules

Enable the EPEL repository and install the latest strongSwan package (typically 5.9.x on CentOS Stream 9):

dnf install -y epel-release
dnf install -y strongswan

After a kernel update, the system occasionally fails to auto-load the necessary IPsec crypto modules. Load them manually right away:

modprobe af_key esp4 xfrm4_mode_tunnel
lsmod | grep -E 'esp4|xfrm'

2. Enable IP Forwarding

Linux drops packet forwarding between network interfaces by default. Without this setting, even if the tunnel reports ESTABLISHED, hosts on both LAN ends will remain completely disconnected:

cat <<EOF > /etc/sysctl.d/99-ipsec.conf
net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
EOF

sysctl --system

3. Build a Precise swanctl Configuration

Open /etc/strongswan/swanctl/swanctl.conf on the Hanoi Gateway. Define the exact cryptographic parameters and subnet ranges for both sides:

connections {
    site-to-site {
        version = 2
        proposals = aes256-sha256-modp2048
        local_addrs = 203.0.113.10
        remote_addrs = 198.51.100.20

        local {
            auth = psk
            id = 203.0.113.10
        }
        remote {
            auth = psk
            id = 198.51.100.20
        }

        children {
            lan-subnets {
                local_ts = 192.168.10.0/24
                remote_ts = 192.168.20.0/24
                esp_proposals = aes256-sha256
                start_action = start
                dpd_action = restart
            }
        }
    }
}

secrets {
    ike-site {
        id-1 = 198.51.100.20
        secret = "VpNS3cr3tK3y@2026Secure!"
    }
}

Switch to the Da Nang Gateway and apply the exact same configuration, simply swapping the value pairs: invert local_addrs with remote_addrs, local_ts with remote_ts, and assign Hanoi’s WAN IP to id-1.

4. Resolve the Firewalld NAT Trap

This was the root cause behind the 2:00 AM tunnel breakdown. Following the package upgrade, firewalld restarted and activated default NAT masquerading rules. As a result, LAN-to-LAN packets were forcefully NAT’d to the public WAN IP rather than routed into the IPsec tunnel.

First, open the required IKE negotiation ports and the ESP protocol:

firewall-cmd --permanent --add-service=ipsec
firewall-cmd --permanent --add-port=500/udp
firewall-cmd --permanent --add-port=4500/udp
firewall-cmd --permanent --add-protocol=esp

Next, add an explicit bypass rule (Bypass Masquerade) for traffic flowing between both private subnets:

firewall-cmd --permanent --direct --add-rule ipv4 nat POSTROUTING 0 -s 192.168.10.0/24 -d 192.168.20.0/24 -j ACCEPT
firewall-cmd --reload

5. Start the Service and Verify Tunnel Health

Enable the systemd unit, load all configuration profiles into the charon daemon, and initiate the Child SA:

systemctl enable --now strongswan
swanctl --load-all
swanctl --initiate --child lan-subnets

Check the Security Associations status:

swanctl --list-sas

Once you observe state = ESTABLISHED alongside steadily incrementing inbound/outbound packet counters, the connection is fully operational:

site-to-site: #1, ESTABLISHED, IKEv2, 70b6..._i c3a1..._r
  local  '203.0.113.10' @ 203.0.113.10[500]
  remote '198.51.100.20' @ 198.51.100.20[500]
  AES_CBC-256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048
  lan-subnets: #1, reqid 1, INSTALLED, TUNNEL, ESP:AES_CBC-256/HMAC_SHA2_256_128
    installed 120s ago, rekeying in 3400s
    in  c1a2b3c4,   14280 bytes,   120 packets
    out c5d6e7f8,   18920 bytes,   155 packets
    local  192.168.10.0/24
    remote 192.168.20.0/24

Run a ping test to verify actual latency between both internal servers:

ping -c 4 192.168.20.1
# 4 packets transmitted, 4 received, 0% packet loss, time 3004ms, rtt avg 14.821 ms

Key Takeaways

The clock hit 3:45 AM. All 15,000 backlogged ERP records from the Da Nang branch synced seamlessly back to Hanoi, well ahead of the morning shift’s arrival.

Most post-reboot IPsec failures on RHEL or CentOS Stream 9 boil down to three usual suspects: forgetting to enable net.ipv4.ip_forward, minor proposal mismatches between endpoints, or firewalld swallowing traffic with Masquerade rules. By mastering the swanctl syntax and knowing how to inspect XFRM policies (ip xfrm policy), you can manage your network infrastructure with total confidence—even in the dead of night.

Share: