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

# Set up a route-based VPN

This tutorial uses the result of the [Policy-based VPN](https://docs.cennso.com/components/cgw/latest/tutorials/02-setup-vpn-ipsec.md) tutorial as a starting point. It is recommended that you read this tutorial first.

## Current configuration

This is the resulting configuration from the policy-based tutorial. This gets changed further to get to a working configuration with the 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

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

## Changes to strongSwan

Use a Virtual Tunnel Interface (VTI) as a target for routing through the tunnel.
Change the configuration as follows:

```yaml
ipsec:
  strongswan:
    conn second-site
      mark=42 # Sets the value used to mark the packets to use in conjunction with VTI
```

If you have multiple VPN connections, make sure that the numbers used as a value for the **mark** parameter are unique to this configuration.

### Subnets

In the case of a route-based VPN, it is common to set both subnets to `::/0` or `0.0.0.0/0`, and decide based on the routes which traffic to send over link.
Therefore, change the networks to the following:

```yaml
ipsec:
  strongswan:
    conn second-site
      leftsubnet=::/0
      rightsubnet=::/0
```

Often, other routers do not accept the setting, so it is reasonable to restrict the networks further as in the starting configuration on the top.

## Setting up VTIs

To route traffic over the VPN connection, you must create a VTI. As described in the tutorial for [imperative IP configuration](https://docs.cennso.com/components/cgw/latest/tutorials/01-imperative-ip-configuration.md), use the init script:

```yaml
initscript:
  enabled: true
  env:
    MARK: "42"

    LEFT_ENDPOINT: "2001:db8:0:1::1"
    RIGHT_ENDPOINT: "2001:db8:0:2::1"

    VIRTUAL_ADDRESS_LEFT_CIDR: "fd2d:3ea6:d58c:f2c9::0/127"
    VIRTUAL_ADDRESS_RIGHT: "fd2d:3ea6:d58c:f2c9::1"

    #<add routing variables here>

  script: |
    set -xeu

    echo "add VTI interface\n"
    ip tunnel add vti${MARK} mode vti local $LEFT_ENDPOINT remote $RIGHT_ENDPOINT key $MARK
    ip -6 address add $VIRTUAL_ADDRESS_LEFT_CIDR dev vti${MARK}
    ip link set vti${MARK} up
    #<add routing rules here later>
```

In the **env** section of the initscript, define your parameters as environmental variables, as it eases reusability and increases readability.

The value `MARK` is used to the define the key for the packets traversing the interface. This values has to be the same as the one configured in the strongSwan section before.

`LEFT_ENDPOINT` and `RIGHT_ENDPOINT` are the endpoints of the VPN connection as configured in the strongSwan section.

`VIRTUAL_ADDRESS_LEFT_CIDR` and `VIRTUAL_ADDRESS_RIGHT_CIDR` are two IP addresses which are used for routing decisions by the kernel. As these IP addresses are not seen on the other side, you just have to use them out of a range, which is not used in you network already. In this case, use ULA IP addresses.

As shown in line 16, create the VTI interface as a virtual tunnel interface with the actual endpoints of the tunnel. Further, the **key** parameter is here set to the value of `MARK`.

Add the virtual IP address for routing to the interface in line 17.

Lastly, enable the tunnel by setting it in the `up` state.

## Add routes

To transfer traffic over the tunnel, add targets and their routes to the routing table.
Firstly, add the following variable to the configuration (line 12 in the configuration above):

```
ROUTED_NETWORK: "2001:db8:10::/48"
```

Secondly, add the following routes at the place marked above in line 19:

```
echo "add routes for VPN\n"
ip -6 route add ${ROUTED_NETWORK} via ${VIRTUAL_ADDRESS_RIGHT}

```

This setting sends the traffic with target of `2001:db8:10::/48` over the VPN tunnel. The right endpoint does not need to have the address assigned to an interface, as it is just virtual on our side.
