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
Post a Comment