Skip to content

Instantly share code, notes, and snippets.

@fardjad
Last active January 17, 2024 16:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fardjad/4e6d12bea31f431638a9e5f0e370de68 to your computer and use it in GitHub Desktop.
Save fardjad/4e6d12bea31f431638a9e5f0e370de68 to your computer and use it in GitHub Desktop.
[DSL/LTE Auto-switch Setup] #openwrt #dsl #lte #multiwan

DSL/LTE Auto-switch Setup

Synopsis

This is how I setup my home router to share a 4G/LTE connection at day and switch to a DSL connection overnight.

It also falls back to DSL connection whenever the 4G/LTE connection dies.

Why?

A 4G connection is generally faster than a normal ADSL connection where I live and my DSL provider charges me for the Internet traffic at day (overnight usage is free.) On the other hand, not only the unlimited traffic plans are super expensive but also they're doomed with what they call, fair usage policy (about 40GBs per month) which IMO is obtuse.

So my idea is to use a 4G/LTE connection at day and switch to the DSL connection overnight.

I also setup a non-caching proxy server so that I can configure my download manager to use that for downloading files. The router always routes the outgoing traffic originating from the proxy server through the DSL gateway. That way I can always surf the internet and download files simultaniously without any disturbance.

Requirements

  1. A DSL modem
  2. An OpenWrt compatible router
  3. An LTE/4G modem

Roles

In my current setup:

  1. DSL modem only provides the OpenWrt router with PPP connection over ethernet
  2. The whole magic happens in the OpenWrt router (including the non-caching proxy server)
  3. The LTE/4G modem/router is responsible for providing the data connection to the OpenWrt router and WiFi to the clients.

DSL Modem Config

I set the DSL modem connection mode to bridge and connected it to the WAN port of my OpenWrt router.

LTE/4G Modem/Router Config

I configured it as I normally would. I set its IP address to 192.168.1.1 and configured the internal DHCP server to assign the clients IP addresses in range of 192.168.1.100-200 and set the default gateway to 192.168.1.2!

Finally I connected the modem to port 0 of my OpenWrt router.

OpenWrt Route

Installed packages:

  • privoxy
  • mwan3

VLANs and Interfaces:

VLAN ID Ports Network Firewall Zone
1 1,2,3 br-lan lan
2 4 wan wan
3 0 wan2 wan2

I assigned the IP address of 192.168.2.1 to the router (br-lan network) And configured the wan network to connect through the DSL modem and set its gateway metric to 20

I also disabled the DHCP server on wan2 network and assigned the static IP address of 192.168.1.2, the default gateway of 192.168.1.1, and the gateway metric of 10 to that interface.

Firewall Config:

Zone => Forwardings Input Output Forward Masquerade MSS clamping Allow forward to destination zones Allow forward from source zones
lan => wan wan2 accept accept accept no no wan wan2 wan2
wan => REJECT reject accept reject yes yes - lan wan2
wan2 => lan wan accept accept accept yes yes lan wan lan

Privoxy Config

I modified the listen-address line in /etc/privoxy.conf and set the bind address to 192.168.1.2

Then enabled the privoxy service:

/etc/init.d/privoxy enable
/etc/init.d/privoxy start

mwan3 Config

config rule 'self'
    option src_ip '192.168.2.1'
    option dest_ip '0.0.0.0/0'
    option proto 'all'
    option sticky '0'
    option use_policy 'adsl'

config rule 'all'
    option proto 'all'
    option sticky '0'
    option use_policy 'balanced'
    option src_ip '0.0.0.0/0'
    option dest_ip '0.0.0.0/0'

config interface 'wan'
    option enabled '1'
    list track_ip '8.8.8.8'
    option reliability '1'
    option count '1'
    option timeout '2'
    option interval '5'
    option down '3'
    option up '3'

config interface 'wan2'
    option enabled '1'
    list track_ip '8.8.8.8'
    option reliability '1'
    option count '1'
    option timeout '2'
    option interval '5'
    option down '3'
    option up '3'

config member 'm1'
    option interface 'wan'
    option weight '1'
    option metric '10'

config member 'm2'
    option interface 'wan2'
    option metric '10'
    option weight '1'

config policy 'balanced'
    list use_member 'm1'
    list use_member 'm2'
    option last_resort 'unreachable'

config policy 'adsl'
    list use_member 'm1'
    option last_resort 'unreachable'

I enabled crontab (/etc/init/cron enable) and added the following line to crontab file (crontab -e)

* * * * * /bin/ash /root/switch_int.sh

Then SSH'd to the router and created a file named switch_int.sh in /root:

#!/bin/ash

if [ $(date +"%H") -gt 1 ] && [ $(date +"%H") -lt 21 ]; then
        echo lte
        route del -net default gw 192.168.1.1 netmask 0.0.0.0 dev eth0.3 metric 30 &>/dev/null
        route add -net default gw 192.168.1.1 netmask 0.0.0.0 dev eth0.3 metric 10 &>/dev/null
else
        echo adsl
        route del -net default gw 192.168.1.1 netmask 0.0.0.0 dev eth0.3 metric 10 &>/dev/null
        route add -net default gw 192.168.1.1 netmask 0.0.0.0 dev eth0.3 metric 30 &>/dev/null
fi

Finally I set its executable flag (chmod +x /root/switch_int.sh) and rebooted the router.

License

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment