Community discussions

MikroTik App
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Automatic switching default routes

Thu Feb 22, 2024 11:11 am

Hello

I've setup a simple dual WAN setup, according to this: https://help.mikrotik.com/docs/display/ ... +Balancing

First default route, is coming from a dhcliet from the main ISP. The second rule, with distance of 2, is going through to another ISP. Problem here is, it seems I can't configure the option check-ping with the main default route.

So when the router switches to the backup route, it never comes back to the first link when it comes online again.

Do you have any suggestions on how to solve this?

Many thanks.
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Thu Feb 22, 2024 1:02 pm

post config (less router serial number, any public wanip info )
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Fri Feb 23, 2024 10:36 am

Hey thanks for the answer! What part you think is relevant? It's pretty big and full of many private information that would be time-consuming to redact all.
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Fri Feb 23, 2024 2:35 pm

After rereading, I understand. You use the use default route route (YES) offered in dhcp client but the settings do not provide check-gateway=ping.
My recommendation is to uncheck the default route and add a manual one.

add distance=1 check-gateway=ping dst-address=0.0.0.0/0 gateway=ISP-gateway-IP routing-table=main

What kind of ISP connection do you have?? ( static, dynamic WANIP ?). Is it a public or private IP?
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Fri Feb 23, 2024 3:47 pm

You're right, exactly.

The setup looks like this: an LHGG with LTE interface who pass via passthrough the public dynamic IP to the WAN port of a Chateau device. Basically the LHGG acts as a modem, and all routing happens on the Chateau (router). On this router, the WAN port is configured with dhcp client.

On the LAN, there's a linux device which has two interfaces: one is connected to the LAN bridge and a wifi interface connected to wireless network that provides external connectivity. Of course, the linux device is configured to properly route packets on its wlan0 interfaces via iptables.

I have know disabled the dhcp client option and configured manually the static route. However it does not seems working correctly. What information should I paste here to help me troubleshooting this issue?
2024-02-23_15-28-50.png
The route on the right, is automatically configured route that is created by the dhclient. Works just fine.

The route on the left is the one I created, and it does not work. Ping to 8.8.8.8, for example, is failing. For obvious reasons I can't put any ip addresses on the manually create route because the IP is dynamic.

If I manually setup a stati default route with the IP address that I can see from the dhclient status windows, everything works fine. I'm pretty sure the immediate gateway change every now and then so having this configuration is not ideal.
2024-02-23_15-46-20.png
Why are you asking what kind of ISP? By the way, being LTE, it's a public dynamic IP massively filtered by the ISP (Vodafone). For example, I'm not reachable from the internet, thus I can't expose ports unfortunately. :(

Many thanks.
You do not have the required permissions to view the files attached to this post.
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Fri Feb 23, 2024 6:43 pm

I have followed another route that somehow solved my case.

I have scheduled this script to run every 10 seconds.
# Script to adjust route availability based on DHCP lease status for ether5, with logging

# Check the status of DHCP lease for ether5
:local dhcpStatus [/ip dhcp-client get [find interface="ether5"] status];

# Find the route ID for the route with destination 0.0.0.0/0 and vrf-interface="ether5"
:local routeID [/ip route find where dst-address="0.0.0.0/0" and vrf-interface="ether5"];

# If the DHCP lease is bound, enable the route if it is disabled
:if ($dhcpStatus = "bound") do={
    :if ([:len $routeID] > 0) do={
        /ip route set $routeID disabled=no
        # Log the enabling of the route
        #/log info "DHCP lease is bound. All good."
    }
} else {
    # If the lease is not bound, disable the route
    :if ([:len $routeID] > 0) do={
        /ip route set $routeID disabled=yes
        # Log the disabling of the route
        /log info "DHCP lease is not bound. Disabling LTE default route."
    }
}
Of course, I have the secondary static route configured with distance 99, just in case.

Anyway I've realized that the default route added by the dhcp client will automatically disappear when the lease expires, so the second route will automagically work out of the box. However the lease expires after 1 minute or so. This script improve the switching time, worst case 10 seconds.

What do you think? Is there a better way to achieve this?

Many thanks!
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Fri Feb 23, 2024 6:50 pm

Interesting, so you dont use LTE settings on the chateau?? Just IP DHCP client?
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Sat Feb 24, 2024 12:28 am

No, I was forced to move it in a central position of the house where the signal is not great. So I introduced an LHGG outside pointed directly to the BTS which is in LoS fortunately.

I did not like having a double NAT setup so I configured the LHGG as a simple modem. It almost works just fine, if only i was able to expose ports externally…

Anyway, what do you think about the configuration? To me, seems a little bit dirty. I wonder if there is a better way to do it.

Many thanks!
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Sat Feb 24, 2024 12:56 am

Nm...........
First take note in ip dhcp client STATUS tab of the wanip and gateway IP.

Two Options if the Modem is doing all the work and you simply get client via ETHER5 then simply
a. DO not use default route
b. add an IP route manually,
add check-gateway=ping distance=1 dst-address=0.0.0.0/0 gateway=ether5 routing-table=main

If for some reason, that does not work, meaning that putting ether5 is not accepted by the router and there is no connection,
then change the gateway IP from ether5, to the actual gateway IP showing in the IP DHCP client status.

That should work. Now we need to test if the router can automatically acquire a different IP and more importantly a different gateway IP.
So, then turn off the MODEM wait a couple of minuter and put power back on and see if the router acquires a new wanip and new gateway IP successfully.

If for some reason the new WAN IP and gateway IP shows in IP DHCP client but the gateway IP is not updating on the manual route, we need to additional work.
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Sat Feb 24, 2024 6:30 pm

Hey!

Indeed, putting ether5 in the gateway field of the manually created default route does not work (router accepts it but i don’t have connectivity) and gateway changes every now and then. I mean, could be the same for three hours but then changes. :-(
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Sat Feb 24, 2024 8:04 pm

Okay no problem so using a manual route using gateway IP set to ether5 did not WORK. Thats okay!


Next step is to do the following.
Reading the IP DHCP client settings, STATUS TAB, get the current gateway IP.

THen go to your manual route and remove ether5 and put in the current actual gateway IP and the comment I have included!
add check-gateway=ping distance=1 dst-address=0.0.0.0/0 gateway=current-gateway-IP routing-table=main comment="My WAN"

Then go back to DHCP client, and in the main DHCP tab, select YES for default route.!
Then go to ADVANCED tab, and enter default route distance of 255
Down below in the SCRIPT AREA add the following script.

:if ($bound=1) do={
:local gw $"gateway-address"
/ip route set [ find comment="My WAN" gateway!=$gw ] gateway=$gw
}


Everytime your IP DHCP Client gets a new Binding (wanip and gatewayip) etc, will ensure that the manual route entry for gatewayIP gets updated to the one on the Status Tab.

In plain english, what we are doing with the script is saying, if the ip dhcp client is bound ( which occurs after reboot, or change of IP from provider etc.......) we define the known string variable to be associated with the the variable gw ( if changed new gateway IP).
Then we find where in the config a specific comment occurs, and on that config line, set the current value stored in the parameter 'gateway' to the new value.
We do add the additional step, before that assignment gateway=$gw, to check to ensure its changed, no need to replace anything if the new value at bound is the same as the current value found in the manual ip route config line.
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Sun Feb 25, 2024 11:41 am

First of all, thanks for help!

I'm not sure if I understand correctly: this script will fix the the route acting on the gateway assigned dynamically with the DHCP from the provider. I get that. However, I understand that you're suggesting to enable its automatic creation via the dhcp client. What's the point of having the script?

If I leave everything just as it is:

- Enabled the automatic creation of the default route via DHCP client, primary route with distance = 1.
- A second default route with distance of 99, not so important but > 1.

The situation looks like this: while the LTE connection (provided by the LHGG acting as a modem) is working the default route with distance equals to 1 has the priority, so the traffic goes out trough its default gateway. If the connection drop, after few minutes, this route will be deleted by the dhcp client and the traffic is routed trough the second default route (the one with the 99 priority). Remember that the LTE connection, provided by the DHCP client, is the primary.

The key part is: after few minutes

What I want to achieve, or knowing if there's a better alternative to the script I scheduled above, is to reduce as much as possible the downtime. My script will check every 10 seconds if the DHCP client is bound, and if it's not, it makes the switch.

Many thanks!
 
Mesquite
Member
Member
Posts: 420
Joined: Tue Jan 23, 2024 9:16 pm

Re: Automatic switching default routes

Sun Feb 25, 2024 2:02 pm

Not sure what your saying.
1-The request was simple, you wanted to make use of check-gateway=ping.
2-To do that you need to create a manual route instead of dhcp client ( add default route=yes).
3-We did that, however your ISP does not work using ether5 as the interface, which does in some cases but not yours, fine .
4-We tried using the actual current ISP gateway in the manual rule, and that works until the IP changes or gateway IP changes and the gateway IP is not updated
++++++++++++++++++++++++++++++++++++++++++++++++++++
If the above is all true, then the next step was to ensure that the gateway IP in the manual route is updated upon reboot or change of dynamic IP by the provider.

To do that we go back to DHCP client and say yes add default route with distance of 255 ( will never get used) and put in a script for that route, FOR THE SOLE PURPOSE of grabbing the newgateway IP. Then in the script we ensure we replace the gatewayIP in the manual script created at 2, with the format at 4-.
 
Kartone
just joined
Topic Author
Posts: 21
Joined: Fri Jul 14, 2023 12:15 pm

Re: Automatic switching default routes

Sun Feb 25, 2024 7:37 pm

Thanks for spending time helping me!