Skip to content

Site-to-site IPsec with strongSwan

How to connect two networks over IPsec with strongSwan — config layout, certificate vs PSK auth, traffic selectors, and the failover patterns that survive a transit outage.

A site-to-site IPsec tunnel connects two LANs over an untrusted transit (the public internet, usually) so that hosts on either side can talk as if routed natively. We run these between datacenters, between AWS VPCs and on-prem, and between cloud providers when a peering link is overkill. This article is the strongSwan site-to-site config we use, with notes on traffic selectors, the dead-peer-detection behaviour that matters in production, and how we structure failover when the upstream IPs change.

How to verify

Before configuring, confirm both ends can reach each other on UDP 500/4500 and that the local LAN range is unique (no overlap with the remote side).

ss -lunp | grep -E ':(500|4500)\b'
ip route show
nft list ruleset 2>/dev/null | grep -E 'esp|500|4500'
echo $((0x$(sha256sum < <(echo "$LAN_CIDR")) % 100))   # sanity

If both sides use the same LAN CIDR (the dreaded 192.168.1.0/24 on both ends), no amount of strongSwan tuning will save you — the kernel will not know which interface a packet to 192.168.1.5 belongs on. Re-IP one side or use NAT-traversal with explicit 1:1 NAT inside the tunnel.

What’s happening

Site-to-site IPsec negotiates two layers:

  • IKE Security Association (Phase 1) — authentication of the peers (cert or PSK), key exchange. Lives on UDP 500/4500.
  • Child SA / ESP (Phase 2) — the actual encrypted data path. Uses ESP packets (IP protocol 50) or ESP-in-UDP-4500 if NAT is detected.

Each Child SA has traffic selectors: local_ts and remote_ts. These describe which (source, destination) packet flows enter the tunnel. A tunnel between 10.10.0.0/16 and 10.20.0.0/16 has those exact selectors; anything else gets routed normally (or dropped if you wrote your policy strictly).

strongSwan installs an xfrm policy in the kernel matching these selectors. When a packet matches, the kernel hands it to xfrm for encryption and emits ESP. The remote end’s xfrm reverses the process. There is no virtual interface in classic transport mode — packets ride the kernel’s main routing table, only encrypted on the wire.

The procedure

  1. Install strongSwan and confirm the swanctl unit is running.

    apt install -y strongswan strongswan-swanctl strongswan-pki
    systemctl enable --now strongswan
  2. Decide the auth model. For two sides you control, certificates are the right answer — they survive rotation cleanly and audit better. PSK is acceptable for a short-lived bring-up; replace it before go-live.

  3. Build a small PKI (skip if you have one).

    cd /etc/swanctl
    pki --gen --type ed25519 > private/ca.key
    pki --self --in private/ca.key --type ed25519 \
        --dn "CN=site-ca" --ca --lifetime 3650 > x509ca/ca.crt
    pki --gen --type ed25519 > private/siteA.key
    pki --pub --in private/siteA.key | \
      pki --issue --cacert x509ca/ca.crt --cakey private/ca.key \
          --dn "CN=siteA.example.org" --san siteA.example.org \
          --lifetime 730 > x509/siteA.crt

    Repeat for siteB with its own key + cert.

  4. Write /etc/swanctl/conf.d/site-to-site.conf on Site A.

    connections {
        siteA-to-siteB {
            version = 2
            proposals = aes256gcm16-prfsha384-ecp384
            dpd_delay = 30s
            dpd_timeout = 120s
            rekey_time = 8h
            local_addrs  = %any
            remote_addrs = siteB.example.org
    
            local {
                auth = pubkey
                certs = siteA.crt
                id = siteA.example.org
            }
            remote {
                auth = pubkey
                id = siteB.example.org
            }
            children {
                net {
                    local_ts  = 10.10.0.0/16
                    remote_ts = 10.20.0.0/16
                    esp_proposals = aes256gcm16-ecp384
                    start_action = trap
                    close_action = trap
                    dpd_action = restart
                }
            }
        }
    }

    On Site B, the same file mirrors the IDs and traffic selectors.

  5. Load the config.

    swanctl --load-all --noprompt
    swanctl --list-conns
  6. Trigger the negotiation. With start_action = trap, the SA comes up on first matching traffic. To force it for testing:

    swanctl --initiate --child net
    swanctl --list-sas

    You should see Phase 1 (IKE) and Phase 2 (CHILD) established.

  7. Add return routes. The kernel routes traffic toward the remote LAN normally; xfrm intercepts based on policy. If your gateway is not the default route, add explicit routes:

    ip route add 10.20.0.0/16 via <next-hop>
  8. Open the firewall and forward.

    ufw allow 500/udp
    ufw allow 4500/udp
    sysctl -w net.ipv4.ip_forward=1
    iptables -A FORWARD -m policy --pol ipsec --dir in  -s 10.20.0.0/16 -j ACCEPT
    iptables -A FORWARD -m policy --pol ipsec --dir out -d 10.20.0.0/16 -j ACCEPT

Operational notes

  • Dead Peer Detection (dpd_delay, dpd_timeout) is non-negotiable. Without it, an upstream blip silently kills the SA and traffic blackholes until the next rekey. Set dpd_action = restart so the tunnel re-establishes automatically.
  • Rekey times affect availability. The default 8h is reasonable; below 1h causes thrashing on some carrier-grade NAT scenarios.
  • Traffic selectors are sets, not single CIDRs. If you need multiple subnets across the tunnel, list them as local_ts = 10.10.0.0/16,10.30.0.0/16. Some peers (notably older Cisco ASA) only negotiate one selector pair per Child SA; you may need multiple children.
  • NAT-T is automatic but the firewall must open UDP 4500. We see this missed on outbound firewalls every other engagement.
  • For failover when the remote IP is dynamic (DDNS, multi-WAN), let remote_addrs resolve to a hostname rather than IP and accept the slightly slower handshake.

In the engagements we run, site-to-site IPsec is one tool in the network toolbox — appropriate for connecting legacy network gear that does not speak WireGuard or for compliance regimes that require IPsec specifically. For greenfield builds we usually prefer WireGuard, but when IPsec is the requirement, our Managed Operations practice covers cert rotation, DPD tuning, and the 3 a.m. “the tunnel dropped” tickets.