> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/cgw/1.7/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.7/tutorials/02-setup-vpn-ipsec.md).

# Set up a VPN connection with IPsec

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

In the following, we will go through an example of setting up an IPSEC connection for the CGW Helm Chart.
The focus is to get an understanding of the IPSEC part of the CGW, therefore, we will assume for this tutorial, that the [IP addresses](https://docs.cennso.com/components/cgw/1.7/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 will be part of the `value.yaml` file you create for configuring the CGW chart.
You can combine multiple parts of the configuration, therefore we will not repeat parts set in previous steps.

## 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, it does make sense to 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 in line 7 the connection `sts-base` is configured. Everything which is indented further is part of this configuration (till line 20).

The first part is the default configuration, which will be inherited by the following configurations.

`dpdaction` defines, what will happen, if the other side does not send liveliness messages and so a dead peer is detected. For site to site VPNs it is preferred to set it to `restart` as the CGW will try to create a new connection immediately.

`closeaction` is closely related 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 will not respond even to connection requests from the other side.
- `add` will add 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` will install a trap into the network stack and not do anything immediately. If the first packet, which matches the parameter of the connection though, it will try to create a connection.
- With `start` it will immediately try to open up a connection. This setting is preferred in site to site scenarios.

`ike` and `esp` are setting 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 will support. In the case you are controlling the both ends of the tunnel, you can have a look at [Cryptographic Algorithms](https://docs.cennso.com/components/cgw/1.7/concepts/cryptographic_algorithms.md) or just stick with the setting above.

The `ike` parameter is setting the algorithms for the IKE phase (first phase), which is used for the control traffic and key exchange, whereas the `esp` parameter is setting the algorithms for the ESP part, which encapsulate the actual data traffic.

`keyingretries` defines how often it tries to choose now keys between the peers, when the negotiation fails.
This value can be set to all positive integers or to `%forever`.

`lifetime` and `ikelifetime` defines the duration of the connections, when they are not disconnected my other means.
After this period the ESP or IKE connection will be rekeyed respectively.

`keyexchange` selects, which protocol will be used for 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` are defining, which method is used to authenticate.

#### The `second-site` section

The second part of the configuration stating at line 22 is the actual connection we are creating.

`also` defines, from which connection we inherit all parameters. These will then be extended or overwritten in this configuration.

`leftsubnet` and `rightsubnet` set the subnets, for which traffic will be transferred of the connection or what packets will be 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 a packets are matched by the rules, the packets will be transferred over the VPN.

For further information about differences between Route-based and Policy-based VPN, please have a look at the [concepts](https://docs.cennso.com/components/cgw/1.7/concepts/policy_route_based_vpn.md) or the [tutorial for route-based vpn](https://docs.cennso.com/components/cgw/1.7/tutorials/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 a lot of parameters to separate between 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 the 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 the 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 pre-shared key and `CorrectHorstBatteryStaple` for the PSK itself.

You can also refer to an existing secret by name. This allows 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 the base64 encoded strongswan credentials like stated above, e.g.:

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

Instead of defining `ipsec.strongswan.ipsecSecrets`, refer to the secret like:

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

## Further reading

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