> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/1.8/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/cgw/1.8/tutorials/02-setup-vpn-ipsec.md).

# Set up a VPN connection with IPsec

In the current implementation of CGW, strongSwan is used as the software for VPN termination using the IPsec protocol.

The following example describes how to set up an IPsec connection for the CGW Helm chart.
The focus is to get an understanding of the IPsec part of CGW, therefore, the assumption for this tutorial is that the [IP addresses](https://docs.cennso.com/components/cgw/1.8/tutorials/01-imperative-ip-configuration.md) and interfaces are already set up.

## Prerequisites

To complete this tutorial, you need a configured `inet0` interface which is connected to the Internet and has a public IP address.
The configuration is the following: `inet0: 2001:db8:0:1::1/64`.

All the following configurations are a part of the `values.yaml` file you create for configuring the CGW chart.
You can combine multiple parts of the configuration, therefore parts set in previous steps are not repeated.

## Enable strongSwan

To enable strongSwan to terminate IPsec traffic, set the following:

```yaml
ipsec:
  enabled: true  # Switch on the strongSwan IPsec module
```

## Configure interface

If you have more than one interface in the pod, it does make sense to specify on which interface strongSwan should listen, as it narrows down the area of attacks.
To set the interfaces strongSwan shall bind on, set **ipsec.interfaces** with a comma-separated list of interfaces, for example:

```yaml
ipsec:
  interfaces: "inet0"
```

## Manual configuration of strongSwan

The recommended way for this version of CGW is to use a manual strongSwan configuration.
The framework strongSwan configurations is the following:

```yaml
ipsec:
  manualConfig: true
  strongswan:
    ipsecConfig:
      ipsec.<myconnectionname>.conf: |
        <add your ipsec config here>
    ipsecSecrets:
      ipsec.<myconnectionname>.secrets: |
        <add your ipsec secret here>
```

### The ipsecConfig section

Under **ipsecConfig**, you can add multiple configurations, where each key is the corresponding filename. For debugging purposes, give it a reasonable name.

The content of this key is a block string which is a strongSwan configuration file. For all details of possible parameters, read the [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/IpsecConf).

This is a simple configuration for route-based VPN:

```yaml
ipsec:
  manualConfig: true # The default value is false
  strongswan:
    ipsecConfig:
      ipsec.simple_route_based_config.conf: |
        # ipsec.conf - strongSwan IPsec configuration file
        conn sts-base
            dpdaction=restart
            closeaction=restart
            auto=start
            ike=aes256gcm16-prfsha384-curve25519!
            esp=aes256gcm16-prfsha384-curve25519!
            keyingtries=%forever
            forceencaps=no
            lifetime=28800s
            ikelifetime=86400s
            keyexchange=ikev2
            mobike=no
            leftauth=psk
            rightauth=psk

        conn second-site
            also=sts-base
            leftsubnet=2001:db8:1::/48
            left=2001:db8:0:1::1
            rightsubnet=::/0
            right=2001:db8:0:2::1
```

#### The `sts-base` section

Starting from line 7, the `sts-base` connection is configured. Everything indented further is part of this configuration (until line 20).

The first part is the default configuration which is inherited by the following configurations:

- **dpdaction** defines what happens if the other side does not send liveness messages and a dead peer is detected. For site-to-site VPNs, it is preferred to set it to `restart` as CGW tries to create a new connection immediately.
- **closeaction** is closely related to **dpdaction** and defines the action when the other peer ends the connection with a closing message. Also here, `restart` should be used for site-to-site connections.
- **auto** defines how strongSwan behaves when it is started. The options are `ignore`, `add`, `route`, and `start`:

  - `ignore` actually ignores the whole connection and does not respond even to connection requests from the other side.
  - `add` adds the connection and respond to requests from the other side, but not start a connection itself. This is useful in the road warrior scenario.
  - `route` installs a trap into the network stack and not do anything immediately. If the first packet matches the parameter of the connection, it tries to create a connection.
  - With `start` it immediately tries to open up a connection. This setting is preferred in site to site scenarios.

- **ike** and **esp** set the cryptographic algorithms which are preferred or even enforced (when ending with `!`) by strongSwan. Often, these parameters are given to you by the other side, you try to connect to. If this is not the case, and you do not control the other side, you have to negotiate what their router supports. In the case you are controlling both ends of the tunnel, you can have a look at Cryptographic Algorithms or just stick with the setting above. The **ike** parameter sets the algorithms for the IKE phase (first phase), which is used for the control traffic and key exchange, whereas the **esp** parameter sets the algorithms for the ESP part, which encapsulates the actual data traffic.
- **keyingretries** defines how often it tries to choose new keys between the peers, when the negotiation fails. This value can be set to all positive integers or to `%forever`.

- **lifetime** and **ikelifetime** define the duration of the connections when they are not disconnected by other means. After this period, the ESP or IKE connection is re-keyed respectively.
- **keyexchange** selects which protocol is used for the key exchange. If it is your choice, you should use `ikev2`. If the other party is not able to support it, you can choose `ikev1`.
- **mobike** enables or disables the usage of the [MOBIKE](https://wiki.strongswan.org/projects/strongswan/wiki/MobIke) protocol. This is not in general advised for site-to-site links.
- **leftauth** and **rightauth** define the method of authenticatation.

#### The `second-site` section

The second part of the configuration, starting at line 22, is the actual connection we are creating:

- **also** defines from which connection we inherit all parameters. These are then extended or overwritten in this configuration.
- **leftsubnet** and **rightsubnet** set the subnets for which traffic is transferred of the connection or what packets are allowed. In this case, all packets with an IP address from **leftsubnet**, as sender on the left side and as receiver on the right side, will be send thorough the tunnel.

The mode used here is called policy-based routing. When the source and destination of packets are matched by the rules, the packets are transferred over the VPN.

For further information about differences between route-based and policy-based VPN, have a look at the [Policy-based and route-based VPN](https://docs.cennso.com/components/cgw/1.8/introduction/policy_route_based_vpn.md) or the [Tutorial for route-based VPN](https://docs.cennso.com/components/cgw/1.8/tutorials/04-route-based-vpn.md).

**left** and **right** set the IP, which is used on the corresponding nodes. Besides setting a specific IP, it is also possible to set the value to `%any` to allow connections from any IP address. This is common in the road warrior scenario but uncommon in site-to-site VPNs.

#### `left` and `right` prefix

strongSwan uses **left** and **right** as prefixes for many parameters to separate both sides. There is no clear separation which one is the current configured CGW so you can use the same configuration on both sides.

It is very common to use the **left** side for CGW you are configuring, and the **right** side for the "other side".

### The `ipsecSecrets` section

The **ipsecSecrets** part contains the actual secrets for the VPN connection. This can either be a pre-shared key or parameters for the used private key.
The following code snippet shows the case for using a pre-shared key:

```yaml
ipsec:
  strongswan:
    ipsecSecrets:
      ipsec.simple_route_based_config.secrets: |
        # ipsec.secrets - strongSwan IPsec secrets file
        2001:db8:0:2::1 : PSK "CorrectHorseBatteryStaple"
```

The first part of line 6 is the ID of the other peer, which is by default the IP address, but can also be a FQDN, an email address, `%any`, or `%any6`.
`%any` and `%any6` match all IPv4 and IPv6 addresses respectively.

Further follows the mode for the authentication and finally the parameters for it. In this case `PSK` for a pre-shared key and `CorrectHorstBatteryStaple` for the PSK itself.

You can also refer to an existing secret by name. This allows you to not expose credentials in a plain value files but instead use some common secret management tools to deploy the secret. The secret must contain base64-encoded strongSwan credentials like stated above, for example:

```yaml
apiVersion: v1
metadata:
  name: psk-secret
data:
  ipsec.test.secrets: MTk4LjE5LjcuNyA6IFBTSyAic2VjcmV0Igo=
kind: Secret
```

Instead of defining **ipsec.strongswan.ipsecSecrets**, refer to the secret in such a way:

```yaml
  strongswan:
    ipsecConfig:
      ipsec.test.conf: |
        conn sts-base
                ike=aes256-sha256-ecp256
                esp=aes256-sha256-ecp256
                leftauth=psk
                rightauth=psk
                ...
    ipsecSecretRef: psk-secret
```

## Next steps

If you want to set up VPN with certificates, go to the [Certificate-based VPN](https://docs.cennso.com/components/cgw/1.8/tutorials/03-setup-certificate-vpn.md) tutorial.
