Skip to main content

Adding a second internet connection to a router with OpenWrt

Contents

Depending on where we use our router, we may be in a situation where we need to have a backup plan in case our primary internet connection goes down.

Personally (March 2020) I am in a situation where my internet connection leaves much to be desired. Despite the fast speed, its reliability drives me crazy (Virgin Media), so I thought about introducing an additional connection. This additional connection will be via a 4G modem connected via USB to my router and is based on the principle that when the primary connection stops working, all traffic will be redirected to the emergency connection.

For this purpose I will use my ASUS RT-AC57U router with OpenWrt software and a ZTE MF823 USB modem (originally from BT, but with changed software and fully unlocked).

What’s more, since this modem has the ability to connect external antennas, I’ve also equipped it with them to boost the signal a bit. The whole thing will use the operator SMARTY, which is based on the 3 network infrastructure in the UK.

And, although network 3 and its coverage leave much to be desired, if it works in my location and provides unlimited data for £20 a month, then it’s not so bad.

So here we go…


Connecting a 4G modem via USB to our router

First, we need to install the necessary packages. We can do this using the graphical interface from a web browser (System > Software) or from an SSH terminal using the following command:

opkg update
opkg install kmod-usb-net-rndis kmod-usb-net kmod-usb2 usb-modeswitch kmod-usb-net-cdc-ether

Then we insert the activated SIM card into our modem and connect the modem to the USB port of our router.

Using the dmesg command we check if the system has detected a new device, in my case:

[1383.238310] usb 1-2: new high-speed USB device number 2 using xhci-mtk
[1388.676158] usb 1-2: USB disconnect, device number 2
[1389.358290] usb 1-2: new high-speed USB device number 3 using xhci-mtk
[1389.586108] rndis_host 1-2:1.0 usb0: register 'rndis_host' at usb-1e1c0000.xhci-2, RNDIS device

From a web browser, go to Network > Interfaces on our router and click Add new interface button.

In the case of my modem, which is detected as a network card via USB, I enter the name 4G, select the protocol, DHCP client and the interface as usb0.

Some modems may show up as eth2 network card instead of usb0.

OpenWrt Add Interface

In the next step we will leave everything default and focus only on the Firewall Settings, where we will add our wan, wan6. Then we save the changes and in the next step click Save & Apply.

OpenWrt Interfaces Firewall Settings

If we did everything correctly, our connection should be active.

OpenWrt Network Interfaces 4G

At this stage, you can set in the firewall rules which connections, ports, IP addresses should use this and not another Internet connection (interface).

In my case, as I mentioned at the beginning, I want to set up the ability to use two connections, of which the 4G one will be activated when the primary one drops out.

Before we go any further, let’s check if our connection is working:

ping -c 1 -I usb0 1.1.1.1

And we move on…


Using multiple internet connections

For this purpose we need the mwan3 package and an application that adds the ability to manage via a web browser luci-app-mwan3.

As above, we can install them from System > Software or from SSH terminal:

opkg update
opkg install mwan3 luci-app-mwan3

The basic function of mwan3 is not only the ability to use both internet connections in parallel (load balancing), but also to monitor each of the connections. When one of the connections goes down, the internet traffic is redirected to the one that is working, and this is the option that it needs the most.

After installing the above packages we will receive an additional option in the Network > Load Balancing (or MultiWAN Manager) menu.

OpenWrt Network Load Balancing (MultiWAN Manager)

First we need to configure our interfaces (Interfaces) by adding 4G (here we use the name set in Network > Interfaces).

OpenWrt Load Balancing (MultiWAN Manager) Interfaces

After adding 4G, in the new interface tab (Interface), we mark our interface as enabled (Enabled) and in the Tracking hostname or IP address fields we enter the DNS addresses 1.1.1.1 and 1.0.0.1. Thanks to this, our router will check if our interface is working. When it is not working, traffic will not be directed to it.

OpenWrt Load Balancing (MultiWAN Manager) Interfaces 4G

It is also worth editing our wan and wan6 interfaces, which by default have Google and Cloudflare servers set to check activity.

OpenWrt Load Balancing (MultiWAN Manager) Interfaces

In case of wan we set

1.1.1.1
8.8.8.8
1.0.0.1
8.8.4.4

In case of wan6 (optional) we set

2606:4700:4700::1111
2001:4860:4860::8888
2606:4700:4700::1001
2001:4860:4860::8844

In addition to the sections described above, the options also include:

Tracking method — The way to determine if an interface is up or down. We leave it at ping by default.

Tracking reliability — specifies how many addresses defined in the Tracking hostname or IP address section must respond positively for the interface to be considered up. We leave it at 1 by default.

Ping count — specifies how many ping requests to send in one test. We leave it at 1.

If the response to ping takes a very long time, after what time it should be interrupted - this is determined by Ping timeout. On the other hand, how often ping should be repeated is determined by Ping interval. In the case when ping is successful, after what time the query should be repeated is determined by Failure interval. In my case, I set these three options to 2/5/1 for wan and 4G, respectively. In the case when the interface is marked as down (Offline), after what time the attempt to check activity and restore the state if it turns out that the interface is up is determined by Recovery interval - in my case I set 3 seconds.

There are two more options left to consider, namely Interface down and Interface up.

Interface down will determine after how many errors when performing ping the interface is considered down (Offline), similarly Interface up after how many correct answers ping will be considered up. In my case, since wan is considered more stable (sic!), and 4G, even when it works, can report an error, even when everything is working, I set 4/6 (wan) and 6/8.

In addition, we still need to specify which interface is primary and which is secondary (emergency). We do this using the Metrics option in the primary interface settings. In my case, I set wan to 10 and 4G to 50. We make this change from the Network > Interfaces level by editing the settings of the individual interfaces.

Confirm everything by clicking Save & Apply.


As you probably noticed, Load Balancing (MultiWAN Manager) also contains Members, Policies and Rules tabs, which we need to set appropriately, depending on what we want to achieve.

Members

Here we set our interfaces with an additional division into which one is more important than the other (similar to what we set earlier). So in my case, when I want to achieve automatic use of 4G when my primary connection goes down, I added two entries:

OpenWrt Load Balancing (MultiWAN Manager) Members

Policies

Here we combine our above settings the way they are supposed to work - i.e. wan first and in case of problems 4G.

OpenWrt Load Balancing (MultiWAN Manager) Policies

Rules

As the name says it all, here we set the rules for the transfer. Which transfer should be forwarded through which interface. By default we get defined rules for encrypted www traffic (https) and everything else.

In my case I will set one default rule.

In order not to describe everything too much, we will focus on the protocol, which we will set to all (all); Policy assigned we set to wan_4G.

OpenWrt Load Balancing (MultiWAN Manager) rules

For all options to be entered correctly, we restart our router or execute the following commands in the terminal:

/usr/sbin/mwan3 restart
/etc/init.d/network restart
/etc/init.d/firewall restart

If we want to see how everything is working, check Status > Load Balancing (MultiWAN Manager).

OpenWrt Load balancing (MultiWAN Manager) status interface

OpenWrt load balancing (MultiWAN Manager) status detail


The above configuration does not exhaust the possibilities of configuring mwan3. We can also set the wan and 4G connections to be used in parallel, or redirect (using the appropriate Rules rule) individual network traffic through a specialized interface. There are many applications, and I focused only on my goal.

Please remember that when we decide to connect dynamic DNS (domain to our variable IP) to specify the appropriate rule so that the IP address update takes place only through the interface we use for this purpose.

If you are interested in other examples, I encourage you to visit the entry braian87b/mwan3-notes.md and the detailed configuration example braian87b/mwan-config.sh.


Credits:
Failover to LTE using mwan3
Connect 4G USB modem to WRT2200

Share on Threads
Share on Bluesky
Share on Linkedin
Share via WhatsApp
Share via Email

Comments & Reactions

Categories