Page 1 of 1

Route not working using interface as gateway

Posted: Mon Nov 25, 2019 6:17 pm
by rbuserdl
Hello,

I recently created some mangle rules because I never could access from outside using the secondary Internet connections
I have one ISP in WAN1 interface and the other in WAN5 interface
If I create the rute, using the default gateway as default gateway it works, but if I set directly WAN5 (the interface) as gateway, it does not work.
Both ISPs give me dynamic IPs and I cant change this, so if I set the route using dg, it wont work when the dg be different
Any Idea?

Just in case I attach the mangle rules and the route created with the interface as gateway:

/ip firewall mangle
add action=mark-connection chain=prerouting connection-mark=no-mark in-interface=WAN1 new-connection-mark=WAN1_conn passthrough=yes
add action=mark-connection chain=prerouting connection-mark=no-mark in-interface=WAN5 new-connection-mark=WAN5_conn passthrough=yes
add action=mark-routing chain=prerouting connection-mark=WAN1_conn dst-address=!192.168.1.0/24 in-interface-list=!WANs new-routing-mark=to__WAN1 \
    passthrough=yes src-address=192.168.1.0/24
add action=mark-routing chain=prerouting connection-mark=WAN5_conn dst-address=!192.168.1.0/24 in-interface-list=!WANs new-routing-mark=to__WAN5 \
    passthrough=yes src-address=192.168.1.0/24
add action=mark-routing chain=output connection-mark=WAN1_conn dst-address=!192.168.1.0/24 new-routing-mark=to__WAN1 passthrough=yes
add action=mark-routing chain=output connection-mark=WAN5_conn dst-address=!192.168.1.0/24 new-routing-mark=to__WAN5 passthrough=yes

/ip route
add check-gateway=ping distance=1 gateway=WAN5 routing-mark=to__WAN5
192.168.1.0/24 is the local network
The route appears as active (in black color) but it does not work.

Regards,
Damián

Re: Route not working using interface as gateway

Posted: Mon Nov 25, 2019 6:41 pm
by sindy
An interface can only be used as a gateway if it is a point-to-point (tunnel) one (actually, there is a mechanism where routers in a subnet advertise their routing functionality to clients using broadcast ICMP, but it doesn't seem to be working in RouterOS even if the gateway supports it).

So when you want to use the route provided by DHCP at WAN 5 in some other routing table than main, you have to use a script at the dhcp-client to update that route's gateway item each time the DHCP server assigns a new IP address of the gateway, something like:
/ip dhcp-client
add interface=WAN5 script="{\
    \n  :local routeId [/ip route find distance=1 dst-address~\"0.0.0.0/0\" routing-mark=to__WAN5]\
    \n  if ((\$bound=1) and ([ip route get \$routeId gateway]!=\$\"gateway-address\")) do={\
    \n    /ip route set \$routeId gateway=\$\"gateway-address\"\
    \n  }\
    \n}"
As you can see, the default route with routing-mark=to__WAN5 has be created manually in advance, as the script only updates the gateway field of an existing one. So don't remove the route with gateway=WAN5 before adding that script to the DHCP client, it will replace the interface by IP address automatically at the nearest DHCP renewal (which you can enforce using /ip dhcp server release [find interface=WAN5]).

If you want the default route via WAN5 to be available only with a routing-mark, set the add-default-route property of that /ip dhcp client to no.

Re: Route not working using interface as gateway

Posted: Mon Nov 25, 2019 8:00 pm
by rbuserdl
Sindy, thanks a lot!!!

I didn't know that, but it make sense. I just remembered that sometimes I had used the route with the interface as gateway, but surely that was with a GRE tunnel.
We usually try to avoid scripts to make it easier for everyone (I have co-workers with less knowledge about mikrotik), but I will try your script this time. Do I need to change something in the script or I just need to copy the code in the "Advance" tab, into the "Script" box? (Graphical interface)
I though that because the script says "add", this will create a new route.

Regards,
Damián

Re: Route not working using interface as gateway

Posted: Mon Nov 25, 2019 8:21 pm
by sindy
The add is for the whole new /ip dhcp-client row because it's a slightly modified export of an existing setup I use in a similar situation, to avoid typos. So remove the existing dhcp-client at WAN5 and then paste the code I gave into a command line window.

Re: Route not working using interface as gateway

Posted: Mon Nov 25, 2019 10:42 pm
by rbuserdl
Ahhh, sorry, I didnt reallice.
Thanks again!!!
I will test and will let you know.

Regards!
Damián

Re: Route not working using interface as gateway

Posted: Mon Nov 25, 2019 10:47 pm
by sindy
I've fixed a typo there (one extra underscore).

There is no way to avoid a script here - you cannot set a routing-mark to use when adding teh default route directly into the /ip dhcp-client configuration.

Re: Route not working using interface as gateway

Posted: Wed Dec 04, 2019 9:27 pm
by rbuserdl
Yes, thank you Sindy,
I did not tell you but I had realliced about the underscore.
I have created the DHCP client with the correction and this changed the route
The IP addess is still the same than before but I trust you ;)

Thanks a lot!

Re: Route not working using interface as gateway

Posted: Wed Dec 04, 2019 10:34 pm
by sindy
I have created the DHCP client with the correction and this changed the route
The IP addess is still the same than before but I trust you ;)
I'm not sure I understand the remark. If the DHCP server always assigns the same gateway IP address, you don't need the script at DHCP client to update the marked route in configuration, but are you sure the ISP won't ever re-arrange their network?

Re: Route not working using interface as gateway

Posted: Fri Dec 06, 2019 10:50 pm
by rbuserdl
Hello Sindy,

What I mean is that the default gateway, so far, is the same than before. Maybe the DG will change in the future.
So, I cannot see that your solution is fine, until the DG change, but I believe that this solution will work because were you who gave me this ;)

Thanks again.
Regards,
Damián