Skip to main content

Mikrotik - Split Tunnel VPN to circumvent ISP Peering Issues

For the past week, my ISP has been struggling with connectivity to resources hosted on the Cloudflare network. The issues manifested as:

  • Packet loss
  • High latency
  • Low download speeds (despite upload speeds remaining unaffected)
To mitigate this, I set up a split tunnel VPN to route all Cloudflare traffic through a WireGuard connection on my MikroTik router. Here is a quick guide on the setup.

First, obtain a WireGuard configuration file from your VPN provider. Ensure this file contains all necessary connection parameters, including the private key, endpoint address, and public key.

Below is an example of the typical configuration data you will need:
 [Interface]  
 PrivateKey = <private-key-data>  
 Address = 10.2.0.2/32  
 DNS = 10.2.0.1  
 
 [Peer]  
 PublicKey = <public-key-data>  
 AllowedIPs = 0.0.0.0/0, ::/0  
 Endpoint = <vpn_provider_endpoint_address>:<vpn_provider_endpoint_port>  
 PersistentKeepalive = 25  
Create the WireGuard interface on the router. Be sure to replace the private-key with the one from your provider's config file:
 [archy@MikroTik] > interface/wireguard/add name=wg-vpn-provider comment="wireguard vpn provider setup" listen-port=51820 mtu=1420 private-key="${private_key_from_the_wireguard_config_file_here}"  
Next, assign the IP address expected by the VPN provider to your new interface:
 [archy@MikroTik] > ip/address/add address=10.2.0.2/30 interface=wg-vpn-provider network=10.2.0.0 comment="wireguard vpn provider network"  
In order to establish the tunnel, add the VPN endpoint as a peer:
 [archy@MikroTik] > interface/wireguard/peers/add allowed-address=0.0.0.0/0 endpoint-address=${endpoint_address_from_wireguard_config} endpoint-port=51820 interface=wg-vpn-provider persistent-keepalive=25s public-key="${public_key_from_wireguard_config}"  
At this point, the WireGuard tunnel should be active. You can verify connectivity by pinging the gateway:
 [archy@MikroTik] > tool/ping address=10.2.0.1 count=1 interval=1 src-address=10.2.0.2  
Before routing traffic, we need to define which networks should use the tunnel. We will add all Cloudflare subnets to an address list named external-cloudflare-networks. We also need to define our local networks in internal-networks for NAT purposes:
 [archy@MikroTik] > ip/firewall/address-list/add address=173.245.48.0/20 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=103.21.244.0/22 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=103.22.200.0/22 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=103.31.4.0/22 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=141.101.64.0/18 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=108.162.192.0/18 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=190.93.240.0/20 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=188.114.96.0/20 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=197.234.240.0/22 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=198.41.128.0/17 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=162.158.0.0/15 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=104.16.0.0/13 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=104.24.0.0/14 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.64.0.0/13 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=131.0.72.0/22 comment=external-cloudflare-networks list=external-cloudflare-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.31.0.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.31.100.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.31.200.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.19.243.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.16.202.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.16.1.0/24 comment=internal-networks list=internal-networks  
 [archy@MikroTik] > ip/firewall/address-list/add address=172.16.0.0/24 comment=internal-networks list=internal-networks  
Since we do not control the routing table at the VPN endpoint, we must masquerade our internal traffic so it appears to come from the WireGuard interface IP:
 [archy@MikroTik] > ip/firewall/nat/add action=masquerade chain=srcnat comment="srcnat to circumvent cloudflare peering issues" dst-address-list=external-cloudflare-networks out-interface=wg-vpn-provider src-address-list=internal-networks  
To avoid polluting the main routing table, we will create a separate table specifically for the VPN traffic:
 [archy@MikroTik] > routing/table/add name=vpn-provider comment="vpn provider wireguard split tunnel routing table" fib  
Now, add the routes for the Cloudflare networks to this new table:
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=173.245.48.0/20 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=103.21.244.0/22 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=103.22.200.0/22 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=103.31.4.0/22 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=141.101.64.0/18 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=108.162.192.0/18 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=190.93.240.0/20 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=188.114.96.0/20 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=197.234.240.0/22 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=198.41.128.0/17 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=162.158.0.0/15 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=104.16.0.0/13 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=104.24.0.0/14 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=172.64.0.0/13 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
 [archy@MikroTik] > ip/route/add disabled=no distance=1 dst-address=131.0.72.0/22 gateway=10.2.0.1 pref-src="" routing-table=vpn-provider scope=30 target-scope=10  
Finally, use routing rules to force traffic destined for the Cloudflare subnets to look up routes only in our custom 'vpn-provider' routing table:
 [archy@MikroTik] > routing/rule/add dst-address=173.245.48.0/20 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=103.21.244.0/22 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=103.22.200.0/22 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=103.31.4.0/22 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=141.101.64.0/18 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=108.162.192.0/18 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=190.93.240.0/20 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=188.114.96.0/20 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=197.234.240.0/22 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=198.41.128.0/17 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=162.158.0.0/15 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=104.16.0.0/13 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=104.24.0.0/14 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=172.64.0.0/13 action=lookup-only-in-table table=vpn-provider  
 [archy@MikroTik] > routing/rule/add dst-address=131.0.72.0/22 action=lookup-only-in-table table=vpn-provider  
The configuration is now complete. Packet loss should be eliminated, and you should see improved throughput when testing with Cloudflare's Speedtest.

Feel free to comment and / or suggest a topic.

Comments

Popular posts from this blog

Dynamic DNS with BIND and ISC-DHCP

I personally prefer to work with hostnames instead of ip-addresses. If you have anything like freeipa or active directory, it will do that for you by registering the client you added to your realm to the managed dns and edit the records dynamically. We can achieve the same goal with just bind and isc-dhcp. I'll use a raspberry pi with raspbian 9 for this setup. So here is a quick tutorial on how to configure the isc-dhcp-server to dynamically update bind. First set a static ip to your server. [archy@ddns ~]$ sudo vim /etc/network/interfaces # interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto eth0 iface eth0 inet static address 172.31.30.5 network 172.31.30.0 broadcast 172.31.30.255 netmask 255.255.255.0 ...

Push logs and data into elasticsearch - Part 2 Mikrotik Logs

This is only about the setup of different logging, one being done with Filebeat and the other being done with sending logging to a dedicated port opened in Logstash using the TCP / UDP Inputs. Prerequesites: You'll need a working Elasticsearch Cluster with Logstash and Kibana. Start by getting the Log Data you want to structure parsed correctly. Mikrotik Logs are a bit difficult since they show you Data in the interface which is already enriched with Time / Date. That means a message that the remote logging will send to Logstash will look like this: firewall,info forward: in:lan out:wan, src-mac aa:bb:cc:dd:ee:ff, proto UDP, 172.31.100.154:57061->109.164.113.231:443, len 76 You can check them in the grok debugger and create your own filters and mapping. The following is my example which might not fit your needs. Here are some custom patterns I wrote for my pattern matching: MIKROTIK_DATE \b(?:jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|jun(?:e)?|jul(?...

LACP-Teaming on CentOS 7 / RHEL 7

What is teaming? Teaming or LACP (802.3ad) is a technique used to bond together multiple interfaces to achieve higher combined bandwith. NOTE: every clients speed can only be as high as the single link speed of one of the members. That means, if the interfaces I use in the bond have 1 Gigabit, every client will only have a maximum speed of 1 Gigabit. The advantage of teaming is, that it can handle multiple connections with 1 Gigabit. How many connections depends on the amount of your network cards. I'm using 2 network cards for this team on my server. That means I can handle 2 Gigabit connections at full rate on my server provided the rest of the hardware can deliver that speed. There also exists 'Bonding' in the Linux world. They both do the same in theory but  for a detailed comparison check out this  article about teaming in RHEL7 . To create a teaming-interface, we will first have to remove all the interface configurations we've done on the (soon to be) sla...

FreeIPA - Integrating your DHCPD dynamic Updates into IPA

I recently went over my network configuration and noticed that the dhcp-leases were not pushed into the IPA-DNS yet. So I thought, why not do it now. The setup is very similar to setting it up on a single bind instance not managed by IPA (I've already written a guide about this here ). My setup is done with the following hosts: ipa01.archyslife.lan - 172.31.0.1 inf01.archyslife.lan - 172.31.0.5 First of all, create a rndc-key: [archy@ipa01 ~]$ sudo rndc-confgen -a -b 512 This will create the following file '/etc/rndc-key' [archy@ipa01 ~]$ sudo cat /etc/rndc.key key "rndc-key" { algorithm hmac-md5; secret "secret_key_here=="; }; We also need to make named aware of the rndc-key and allow our remote dhcp server to write dns entries: [archy@ipa01 ~]$ sudo vim /etc/named.conf ... include "/etc/rndc-key"; controls { inet 172.31.0.1 port 953 allow { 172.31.0.5; } keys ...

Creating a pgpool-II based PostgreSQL Cluster

This time I'm going to do a small and quick walkthrough for a postgresql cluster install. I assume you have a clean install of CentOS 7.3 with all updates. The configuration itself is surprisingly simple. The enviroment I'm working with is: Node1: Hostname: pgsql01.archyslife.lan IP: 172.31.10.31 Member of IPA-Domain Selinux: enforcing Node2: Hostname: pgsql02.archyslife.lan IP: 172.31.10.32 Member of IPA-Domain Selinux: enforcing Cluster: Main Node: pgsql01.archyslife.lan Replica: pgsql02.archyslife.lan Virtual IP: 172.31.10.33 for the sake completeness I'll be adding a A-Record entry in the IPA-DNS. Let's start with the configuration of each node. First I will completely setup the Master without restarting the services, afterwards the replica will follow. Steps necessary for both nodes. Add the pgsql-repo to yum. [archy@pgsql01 ~]$ sudo yum -y install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6...