Troubleshooting StrongSwan VPN Tunnel Connectivity Between AWS EC2 Instances: IKE Negotiation and Routing Issues


2 views

When setting up StrongSwan VPN between AWS instances across different regions, we encountered persistent IKE negotiation failures manifesting as "error writing to socket: Invalid argument" errors. The tunnel wouldn't establish despite proper AWS security group configurations and network ACLs.

First, let's examine the critical configuration components that needed verification:

# /etc/ipsec.conf (Base configuration)
config setup
    charondebug="ike 2, cfg 2"

conn aws-vpn
    left=52.Y.Y.Y
    leftsubnet=10.194.0.0/16
    leftid=@oregon-vpn
    right=54.X.X.X
    rightsubnet=10.198.0.0/16
    auto=start
    authby=secret
    keyexchange=ikev2
    ike=aes256-sha2_256-modp2048!
    esp=aes256-sha2_256!
    aggressive=no
    type=tunnel
    dpdaction=restart
    closeaction=restart

The "Invalid argument" socket error typically indicates either:

  1. Incorrect IP addressing in the configuration
  2. NAT traversal issues
  3. Kernel-level networking problems

The solution involved adding explicit NAT traversal parameters:

# Additional parameters in ipsec.conf
conn aws-vpn
    ...
    forceencaps=yes
    nat-ikev1-method=none
    rekey=no

After resolving the initial IKE issue, we encountered routing problems evidenced by ICMP redirect messages:

# Required sysctl settings
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.ip_forward = 1

For the Child SA establishment, we needed to specify proper ESP proposals:

# Complete working ESP configuration
conn aws-vpn
    ...
    esp=aes256-sha2_256-modp2048!
    keyingtries=%forever
    ikelifetime=28800s
    lifetime=3600s
    rekeymargin=3m

The complete solution required these key components:

# /etc/ipsec.secrets
54.X.X.X 52.Y.Y.Y : PSK "YourSharedSecretHere"

# /etc/sysctl.conf additions
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0

Use these commands to verify the tunnel status:

sudo ipsec statusall
sudo ip xfrm state
sudo tcpdump -n -i eth0 udp port 500 or udp port 4500

When working with AWS EC2 instances:

  • Disable source/destination check on both instances
  • Ensure security groups allow UDP 500 and 4500 both ways
  • Configure proper route tables in each VPC
  • Consider using VPC peering for same-region connectivity

When setting up a StrongSwan VPN tunnel between AWS EC2 instances across different regions, many administrators encounter the frustrating "Error writing to socket: Invalid argument" message. This typically appears in /var/log/syslog when the IKE negotiation fails:

charon: 16[NET] error writing to socket: Invalid argument
charon: 08[IKE] giving up after 5 retransmits
charon: 08[IKE] establishing IKE_SA failed, peer not responding

The root cause often lies in the IPsec configuration. Here's a working configuration template for /etc/ipsec.conf:

config setup
    charondebug="ike 2, net 2"
    uniqueids=no

conn %default
    keyexchange=ikev2
    ike=aes128-sha1-modp2048!
    esp=aes128-sha1-modp2048!
    dpdaction=restart
    closeaction=restart
    mobike=no

conn aws-tunnel
    left=52.Y.Y.Y
    leftsubnet=10.194.0.0/16
    leftid=52.Y.Y.Y
    right=54.X.X.X
    rightsubnet=10.198.0.0/16
    rightid=54.X.X.X
    auto=start
    authby=secret

Ensure these sysctl parameters are set in /etc/sysctl.conf:

net.ipv4.ip_forward=1
net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.all.send_redirects=0
net.ipv4.conf.default.rp_filter=0
net.ipv4.conf.eth0.rp_filter=0

When you see "failed to establish CHILD_SA" errors, these additional steps often help:

# Clear existing SAs
ipsec down aws-tunnel
ipsec up aws-tunnel

# Verify with
ipsec statusall

For proper routing between VPCs, ensure:

  • Security groups allow UDP ports 500 and 4500
  • Network ACLs permit the same ports
  • Route tables point to the VPN instances for the remote subnets

After extensive testing, this combination proved most reliable:

conn aws-tunnel
    left=52.Y.Y.Y
    leftsubnet=10.194.0.0/16
    leftid=52.Y.Y.Y
    right=54.X.X.X
    rightsubnet=10.198.0.0/16
    rightid=54.X.X.X
    auto=start
    authby=secret
    keyexchange=ikev2
    ike=aes128-sha256-modp2048!
    esp=aes128-sha256-modp2048!
    dpdaction=restart
    closeaction=restart
    keyingtries=%forever
    type=tunnel