Failover (WAN Backup)
CLI reference:/ip/route/ip/firewall/nat
A router with two internet links can use the second link when the first one fails. This page shows two ways to do it with routes only:
- Check the gateway of each provider. This is simple, but it does not see a failure inside the network of the provider.
- Check hosts on the internet through each link, with recursive routes. This also sees a failure behind the gateway.

In the examples, the first provider (ISP1) is on ether1 with the gateway 10.111.0.1, and the second (ISP2) is on ether2 with the gateway 10.112.0.1. ISP1 is the main link.
Prepare NAT
The devices of the LAN must reach the internet through both links, so each WAN interface needs a masquerade rule:
/ip/firewall/nat
add chain=srcnat action=masquerade out-interface=ether1
add chain=srcnat action=masquerade out-interface=ether2
With the default configuration, the masquerade rule uses the interface list WAN, and the two rules are not needed. Take the second WAN port out of the LAN bridge and add it to the list. The default firewall then also protects it. Without this, the traffic through the second link is not translated and gets no answers:
/interface/bridge/port/remove [find interface=ether2]
/interface/list/member/add list=WAN interface=ether2
The routes of this page are static and need the address of each gateway. If a link gets its settings from a DHCP or PPPoE client, set add-default-route=no in the client, so that its own default route does not compete with the routes of this page.
Check the gateways
Add a default route for each link. The route with the lower distance is used while it is active:
/ip/route
add distance=1 gateway=10.111.0.1 check-gateway=ping
add distance=2 gateway=10.112.0.1 check-gateway=ping
With check-gateway=ping, the router pings the gateway of the route. When the gateway does not answer, the route becomes inactive, and the route with the next distance takes over. When the gateway answers again, the first route becomes active again. Routing decision describes the timing of the check.
In /ip/route/print, the route of ISP1 has the flag A (active). After the gateway of ISP1 stops answering, this route gets the flag I (inactive) and the route of ISP2 becomes active. The router pings the gateway every 10 seconds and needs three failed checks in a row, so this takes about 20 seconds with the default timing.
An answer from the gateway shows only that the link between the router and the gateway works. The gateway can answer while the provider has no connection to the internet, and then the router keeps using a link that does not work.
Check hosts on the internet
To check the path to the internet, ping a host on the internet through each link. Choose two reliable hosts that answer pings, one for each link. This example uses the public DNS servers 8.8.8.8 and 8.8.4.4.
First add a route to each host through its link. Then add the default routes with the hosts as gateways:
/ip/route
add dst-address=8.8.8.8 scope=10 gateway=10.111.0.1
add dst-address=8.8.4.4 scope=10 gateway=10.112.0.1
add distance=1 gateway=8.8.8.8 target-scope=11 check-gateway=ping
add distance=2 gateway=8.8.4.4 target-scope=11 check-gateway=ping
These are recursive routes: the gateway of a default route is not directly connected, and the router finds the way to it through another route. scope and target-scope decide which routes may be used for this: a route resolves its gateway through routes whose scope is not greater than its target-scope. The host routes have scope=10, and the default routes have target-scope=11.
The scope=10 counts. A static route has the default scope of 30, and a host route that kept the default could not resolve the gateway of the route with target-scope=11: the default route would then have no valid next hop and stays inactive. Routing decision lists the default scope and target-scope of connected, IGP, static and BGP routes.
The detail output shows the real next hop of each default route in immediate-gw:
[admin@Router] > /ip/route/print detail where dst-address=0.0.0.0/0
Flags: A - ACTIVE; s - STATIC
0 As dst-address=0.0.0.0/0 routing-table=main gateway=8.8.8.8
immediate-gw=10.111.0.1%ether1 check-gateway=ping distance=1 scope=30
target-scope=11
1 s dst-address=0.0.0.0/0 routing-table=main gateway=8.8.4.4
immediate-gw=10.112.0.1%ether2 check-gateway=ping distance=2 scope=30
target-scope=11
- When the host of ISP1 stops answering, the first default route becomes inactive in about 20 seconds, the same timing as the gateway check, and the traffic uses ISP2, also when the gateway of ISP1 still answers. When the host answers again, the route becomes active again.
- The host routes are necessary. Without them, both default routes are inactive.
- Each host is reachable only through its own link. Do not use an address that your network needs through the other link, for example the DNS server that the LAN uses.
Check two hosts for each link
With one host for each link, losing that host alone switches the traffic to the backup link. To keep the main link while at least one of its hosts answers, give each link two hosts: 8.8.8.8 and 208.67.222.222 for ISP1, 8.8.4.4 and 208.67.220.220 for ISP2. The host routes of the first pair already exist. Add the second pair:
/ip/route
add dst-address=208.67.222.222 scope=10 gateway=10.111.0.1
add dst-address=208.67.220.220 scope=10 gateway=10.112.0.1
Replace the two default routes of the previous section with four, two for each link:
/ip/route
remove [find dst-address=0.0.0.0/0]
add distance=1 gateway=8.8.8.8 target-scope=11 check-gateway=ping
add distance=1 gateway=208.67.222.222 target-scope=11 \
check-gateway=ping
add distance=2 gateway=8.8.4.4 target-scope=11 check-gateway=ping
add distance=2 gateway=208.67.220.220 target-scope=11 \
check-gateway=ping
The two routes with the distance 1 are one ECMP route (flag +) while both hosts of ISP1 answer. When one host stops answering, the other route of ISP1 stays active and the traffic keeps using ISP1. The traffic moves to ISP2 only when both hosts of ISP1 do not answer, through the active route with the distance 2.
What happens to open connections
A connection that is open when the route changes can break. The devices open it again. New connections use the link of the active route.
To use both links at the same time, see Per connection classifier.