Shifting/pre-emption from WAN to LTE in OpenWrt when Wireguard tunnel is enabled

Currently I am facing an issue, where the OpenWRT router is not shifting to LTE when the WAN cable is connected and does not have internet connection when Wireguard tunnel is active.
I am looking for help from the community where:
I want to know how the router can automatically shift from WAN to LTE, when internet on the WAN is not active and establish the tunnel with LTE and take all the traffic.

When the internet is back on the WAN, the tunnel should be shifted back to WAN cable and take all the traffic, shifting LTE on 2nd priority.

I am looking for a preemption method, where we will be utilizing WAN cable as much as possible and not utilizing the data of LTE in case the wan is available.

You may want to look at mwan3 package.

Hi @trendy ,

Thanks a lot for your reply.
I have tried to setup mwan3 and also gave high priority to the WAN cable. But still the pre-emption is not working.

Can this be done by using bash scripting and can also you tell me the way to properly setup mwan3?

mwan3 does not interrupt existing connections, so I believe you need to ifup your WireGuard interface if wan goes up (so it doesn't continue to use the LTE wan interface it failed-over to before). That's quite easy to do, either within the /etc/mwan3.user shell script, or as a standalone shell script in /etc/hotplug.d/iface. It goes something like this:

[ "$ACTION" = ifup -a "$INTERFACE" = wan ] && ifup wg0

(untested, standalone script needs the shebang on top of the script of course)

Edit, for posterity: This only works if the wan interface actually notices it's down (e.g., pppoe), otherwise as suggested below one would have to check for the "connect" action, but that only works in mwan3.user, not as standalone hotplug script.

1 Like

Hi @takimata
Thanks a lot for your reply
I have tried out something similar to the script which you mentioned

#place this script in /etc/hotplug.d/iface
#!/bin/sh
[ "$ACTION" = "ifup" -a "$INTERFACE" = "wan" ] && {
    logger "iface wan up detected..."
    ifup modem_1_1_2
}
exit 0

But this script is only working when I unplug and plug the WAN cable back into to the router.

When the WAN cable is already plugged to the router and the internet is down in the cable the router is not able to detect that the internet is down and not shifting the wg tunnel to the LTE interface and when the internet is back on the WAN cable the router is not able to shift the wg tunnel from LTE back to WAN.

Each time I got to restart the router to make the wg tunnel to shift. I want to the shifting to happen automatically without any restart

You probably want to use the connected/disconnected action.

1 Like

@trendy
Can you please elaborate on your latest reply?

Like.. Is it regarding the bash script or mwan3?

Thanks :slight_smile:

Check /etc/mwan3.user .

2 Likes

Try the following, correcting the interface names:

cat << "EOF" >> /etc/mwan3.user

[ "${INTERFACE}" == "wan" ] && [ "${ACTION}" == "connected" -o "${ACTION}" == "disconnected" ]

/bin/sleep 10; ifup wg0

EOF
1 Like

Hi @pavelgl

Thanks a lot for reaching out to help
I tried to use the script which you mentioned above and the made few changes as you suggested. After using it the wg interface keep restarting every 20 secs. I think the mwan3track is tracking the WAN cable as disconnected even when it is connected. Any way to solve this?

Restarting the wireguard interface seems to provoke some events on the wan interface that I can't explain.

Insert logger -t hotplug.mwan3 $(env) into /etc/mwan3.user just above /bin/sleep 10; ifup ... and check the logs using logread -f -e hotplug.mwan3.

Then comment out everything in /etc/mwan3.user except the last (logging) command added and check the logs again to see if there will be any mwan3 activity on the wan interface.

After I comment out I don't see any activity on the WAN interface. But when I don't comment I see a lot of on the WAN interface.

This is what the logger outputs after I don't comment out the code:

Mon Nov 21 11:49:36 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:49:37 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:49:42 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:49:43 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:49:48 2022 user.notice hotplug.mwan3: ACTION=ifup-failed SHLVL=1 INTERFACE=modem_1_1_2 PWD=/ DEVICE=
Mon Nov 21 11:49:52 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:49:54 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:49:57 2022 user.notice hotplug.mwan3: ACTION=ifup-failed SHLVL=1 INTERFACE=modem_1_1_2 PWD=/ DEVICE=
Mon Nov 21 11:50:02 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:02 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:06 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:07 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:13 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:14 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:18 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:19 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:25 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:26 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:32 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:36 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:37 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=
Mon Nov 21 11:50:44 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth1
Mon Nov 21 11:50:45 2022 user.notice hotplug.mwan3: ACTION=ifdown SHLVL=1 INTERFACE=wan PWD=/ DEVICE=

There is a problem with the conditions test in your hotplug script.

If everything is correct, you should only see logs about the wan interface and the connected/disconnected action.

Mon Nov 21 10:00:57 2022 user.notice hotplug.mwan3: ACTION=connected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth0.2
Mon Nov 21 10:20:19 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/root DEVICE=
Mon Nov 21 10:20:39 2022 user.notice hotplug.mwan3: ACTION=connected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth0.2
Mon Nov 21 14:24:20 2022 user.notice hotplug.mwan3: ACTION=disconnected SHLVL=1 INTERFACE=wan PWD=/root DEVICE=
Mon Nov 21 14:44:23 2022 user.notice hotplug.mwan3: ACTION=connected SHLVL=1 INTERFACE=wan PWD=/ DEVICE=eth0.2

Please post the contents of /etc/mwan3.user

1 Like