> 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/introduction/policy_route_based_vpn.md).

# Policy-based and route-based VPN

## Policy-based VPN

By default, strongSwan and the older models of IPsec VPNs use the **policy-based routing** mode.

As the name suggests, the corresponding software, in this case strongSwan, adds policies into the kernel. These polices either match your packets and therefore encrypt them and send them over the tunnel to the right destination, or they do not match the packets and therefore don't touch them at all.

On Linux machines, you can see the installed policies using the following command:

```sh
ip -6 xfrm policy
```

Output example:

```
src 2001:db8:1::/48 dst ::/0
        dir out priority 399999
        tmpl src 2001:db8:0:1::1 dst 2001:db8:0:2::1
                proto esp spi 0xbf6dcd82 reqid 2 mode tunnel
src ::/0 dst 2001:db8:1::/48
        dir fwd priority 399999
        tmpl src 2001:db8:0:2::1 dst 2001:db8:0:1::1
                proto esp reqid 2 mode tunnel
src ::/0 dst 2001:db8:1::/48
        dir in priority 399999
        tmpl src 2001:db8:0:2::1 dst 2001:db8:0:1::1
                proto esp reqid 2 mode tunnel
```

These three policies depend on their direction as mentioned in lines 2, 6, and 10. When the source and the destination addresses match the corresponding source and destination range, the packets are treated with the protocol defined in lines 4, 8, and 12 – in this case Encapsulating Security Payload (ESP).

This is commonly used for VPN because no routes have to be set or agreed on.

## Route-based VPN

**Route-based VPN** uses a virtual interface called Virtual Tunnel Interface (VTI). In the case of Linux, this is actually implemented like a tunnel and, from the user perspective, looks like other tunnels, including GRE and VXLAN.

This tunnel interface can therefore be used to route traffic over this interface. Every packet which reaches the interface is marked by the kernel number. After that, the packet is matched by the XFRM rule and therefore encrypted and sent to the other IPsec Gateway.

Comparing to the policy-based VPN, the configuration of a route-based VPN is a bit more complicated because of the created tunnels and routes. The benefit of this solution is that also other routing mechanisms are working over the VPN tunnels, for example BGP and OSPF. That's why it is possible to create high available architectures with failover between tunnels.

Due to these reasons, route-based VPN is the preferred way of setting up VPNs at Travelping.

For further information, read the [strongSwan documentation](https://wiki.strongswan.org/projects/strongswan/wiki/RouteBasedVPN).

## Caveat with VTI

As described above, in the case of Linux, VTIs are not implemented as special interfaces, like in some other operating systems, but as virtual tunnels.

Therefore, use the following command:

```sh
ip tunnel add <name> local <local IP> remote <remote IP> mode vti key <number equalling the mark>
```

For debugging purposes, set `<local IP>` and `<remote IP>` to the same IP as used in the VPN configuration.

To route traffic over to the VTI, assign a network and an IP address to VTI. If the address `2001:db8:100::0/127` is assigned to the `vti1` interface, the corresponding routes are set as follows:

```sh
ip -6 route add ::/0 via 2001:db8:100::1`
```

In this example, it doesn't matter which IP addresses are used, as long as they do not interfere with the ones already used in the network.
Because the packets are marked when going through VTI and the Ethernet header is removed, the IP address of the other side, in this case `2001:db8:100::1/127`, does not need to exists on the other end and is just virtual to us.
