Configuring WireGuard on a Mikrotik Router Running OpenWrt

Configuring WireGuard on a Mikrotik Router Running OpenWrt
In most cases, connecting a router to a VPN is not difficult, but if you want to protect an entire network while maintaining optimal connection speed, the best solution would be to use a VPN tunnel. WireGuard.

Routers Mikrotik have established themselves as reliable and very flexible solutions, but unfortunately there is still no support for WireGuard on RouterOS and it is unknown when it will appear and in what form. Recently, it became known the developers of the WireGuard VPN tunnel proposed a set of patches, which will make their VPN tunnel software part of the Linux kernel, we hope this will facilitate its implementation in RouterOS.

But for now, unfortunately, to set up WireGuard on a Mikrotik router, you need to change the firmware.

We flash Mikrotik, install and configure OpenWrt.

First, you need to ensure that OpenWrt supports your model. You can check the correspondence of the model to its marketing name and image on the mikrotik.com website..

Go to openwrt.com in the firmware downloads section..

For this device, we need 2 files:

downloads.openwrt.org/releases/18.06.2/targets/ar71xx/mikrotik/openwrt-18.06.2-ar71xx-mikrotik-rb-nor-flash-16M-initramfs-kernel.bin|elf

downloads.openwrt.org/releases/18.06.2/targets/ar71xx/mikrotik/openwrt-18.06.2-ar71xx-mikrotik-rb-nor-flash-16M-squashfs-sysupgrade.bin

You need to download both files: Install and Upgrade.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

1. Network setup, downloading and configuring a PXE server

Download Tiny PXE Server for the latest version of Windows.

Extract to a separate folder. In the config.ini file, add the parameter rfc951=1 section [dhcp]. This parameter is the same for all Mikrotik models.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Now let's go to network settings: you need to set a static IP address on one of the network interfaces of your computer.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

IP address: 192.168.1.10
Subnet mask: 255.255.255.0

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Run Tiny PXE Server as Administrator and select in the field DHCP Server server with address 192.168.1.10

In some versions of Windows, this interface may only appear after connecting Ethernet. I recommend connecting the router and directly linking the router and PC with a patch cable.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Click the button "…" (bottom right) and specify the folder where you downloaded the firmware files for Mikrotik.

Select the file whose name ends with "initramfs-kernel.bin or elf"

Configuring WireGuard on a Mikrotik Router Running OpenWrt

2. Booting the router from the PXE server

Connect the PC and the first port (wan, internet, poe in, …) of the router with a cable. After this, take a toothpick, insert it into the hole marked "Reset."

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Turn on the power to the router and wait about 20 seconds, then release the toothpick.
In the next minute, the following messages should appear in the Tiny PXE Server program window:

Configuring WireGuard on a Mikrotik Router Running OpenWrt

If the message appears, you're heading in the right direction!

Restore the settings on the network adapter and set it to obtain an address dynamically (via DHCP).

Connect to the LAN ports of the Mikrotik router (2…5 in our case) using the same patch cable. Just switch it from port 1 to port 2. Open the address 192.168.1.1 in your browser.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Log into the OpenWRT admin interface and navigate to the menu section 'System -> Backup/Flash Firmware'

Configuring WireGuard on a Mikrotik Router Running OpenWrt

In the 'Flash new firmware image' subsection, click the 'Choose File (Browse)' button.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

Specify the path to the file, which should end with '-squashfs-sysupgrade.bin'.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

After that, click the 'Flash Image' button.

In the next window, click the 'Proceed' button. The firmware will begin uploading to the router.

Configuring WireGuard on a Mikrotik Router Running OpenWrt

!!! DO NOT DISCONNECT THE ROUTER'S POWER DURING THE FIRMWARE PROCESS !!!

Configuring WireGuard on a Mikrotik Router Running OpenWrt

After flashing and rebooting the router, you'll have a Mikrotik with OpenWRT firmware.

Possible issues and their solutions

Many Mikrotik devices released in 2019 use the FLASH-NOR memory chip type GD25Q15/Q16. The issue is that when re-flashing, the model data is not preserved.

If you see the error 'The uploaded image file does not contain a supported format. Make sure that you choose the generic image format for your platform.', the flash is likely the problem.

You can easily check this: run the model ID check command in the device terminal

root@OpenWrt: cat /tmp/sysinfo/board_name

And if you receive the response 'unknown', you need to manually specify the device model as 'rb-951-2nd'

To obtain the device model, execute the command

root@OpenWrt: cat /tmp/sysinfo/model
MikroTik RouterBOARD RB951-2nd

Having obtained the device model, set it manually:

echo 'rb-951-2nd' > /tmp/sysinfo/board_name

After that, you can flash the device through the web interface or using the 'sysupgrade' command

Create a VPN server with WireGuard

If you already have a server with WireGuard configured, you can skip this step.
I will use the application for setting up my personal VPN server MyVPN.RUN which I have already published a review about.

Setting up WireGuard Client on OpenWRT

Connect to the router via SSH protocol:

ssh root@192.168.1.1

Install WireGuard:

opkg update
opkg install wireguard

Prepare the configuration (copy the code below into a file, replace the indicated values with your own, and run it in the terminal).

If you're using MyVPN, you only need to change the following in the configuration WG_SERV — server IP, WG_KEY — private key from the wireguard configuration file and WG_PUB — public key.

WG_IF="wg0"
WG_SERV="100.0.0.0" # server IP address
WG_PORT="51820" # wireguard port
WG_ADDR="10.8.0.2/32" # wireguard address range

WG_KEY="xxxxx" # private key
WG_PUB="xxxxx" # public key 

# Configure firewall
uci rename firewall.@zone[0]="lan"
uci rename firewall.@zone[1]="wan"
uci rename firewall.@forwarding[0]="lan_wan"
uci del_list firewall.wan.network="${WG_IF}"
uci add_list firewall.wan.network="${WG_IF}"
uci commit firewall
/etc/init.d/firewall restart

# Configure network
uci -q delete network.${WG_IF}
uci set network.${WG_IF}="interface"
uci set network.${WG_IF}.proto="wireguard"
uci set network.${WG_IF}.private_key="${WG_KEY}"

uci add_list network.${WG_IF}.addresses="${WG_ADDR}"

# Add VPN peers
uci -q delete network.wgserver
uci set network.wgserver="wireguard_${WG_IF}"
uci set network.wgserver.public_key="${WG_PUB}"
uci set network.wgserver.preshared_key=""
uci set network.wgserver.endpoint_host="${WG_SERV}"
uci set network.wgserver.endpoint_port="${WG_PORT}"
uci set network.wgserver.route_allowed_ips="1"
uci set network.wgserver.persistent_keepalive="25"
uci add_list network.wgserver.allowed_ips="0.0.0.0/1"
uci add_list network.wgserver.allowed_ips="128.0.0.0/1"
uci add_list network.wgserver.allowed_ips="::/0"
uci commit network
/etc/init.d/network restart

The WireGuard setup is now complete! All traffic on all connected devices is secured by the VPN connection.

Links

Source #1
Revised instructions on MyVPN (additional instructions for configuring L2TP, PPTP on standard Mikrotik firmware are available)
OpenWrt WireGuard Client

Source: habr.com

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster