Policy Routing
CLI reference:/routing/table/routing/rule
Policy routing steers traffic that matches criteria to a specific gateway. Use it to force selected customers or protocols, such as HTTP traffic, to always use a particular gateway. It can also route local and overseas traffic through different gateways.
RouterOS provides these components for policy routing:
- Routing tables.
- Routing rules.
- Firewall mangle marking.
Routing tables
A router can have multiple routing tables, each with its own routes for the same destination and different gateways.
See and configure tables in the /routing/table menu.
By default, RouterOS has only the main routing table:
[admin@MikroTik] > /routing/table/print
Flags: D - DYNAMIC
0 D name="main" fib
Define a custom routing table in this menu before using it elsewhere in the configuration.
Consider a basic example with two gateways, 172.16.1.1 and 172.16.2.1. The main table reaches 8.8.8.8 through 172.16.1.1, and the routing table myTable reaches it through 172.16.2.1:
/routing/table/add name=myTable fib
/ip/route/add dst-address=8.8.8.8 gateway=172.16.1.1
/ip/route/add dst-address=8.8.8.8 gateway=172.16.2.1@main \
routing-table=myTable
The gateway of a route in a user table is resolved through the main table, also without a suffix, so
172.16.2.1 only has to be reachable in main. The @main suffix states this explicitly. Check the routes of the new table:
[admin@MikroTik] > /routing/route/print where routing-table=myTable
Flags: A - ACTIVE; s - STATIC
Columns: DST-ADDRESS, GATEWAY, AFI, ROUTING-TABLE, DISTANCE, SCOPE,
TARGET-SCOPE, IMMEDIATE-GW
DST-ADDRESS GATEWAY AFI ROUTING-TABLE D SCOPE TA IMMEDIATE-GW
As 8.8.8.8/32 172.16.2.1 myTable 1 30 10 172.16.2.1%ether2
The previous configuration alone does not send any traffic to the new table. RouterOS provides two options:
- Firewall mangle gives more control over the criteria used to steer traffic, such as per-connection or per-packet balancing. For examples, see Firewall Marking.
- Routing rules have a basic set of parameters for steering traffic quickly. This example uses routing rules.
Do not use both methods at the same time unless necessary. If mangle and routing rules are used together, mangle has the higher priority with the default policy-rules order: traffic that a mangle rule marks for a table that can resolve it never reaches the routing rules. The order can be changed.
Routing table count is limited to 4096 unique tables.
Routing rules
Routing rules steer traffic based on basic parameters such as source address, destination address, in-interface, and others.
In this example, select traffic destined for 8.8.8.8 and prevent fallback to the main table:
/routing/rule/add dst-address=8.8.8.8 action=lookup-only-in-table \
table=myTable
If a customer is connected to ether4 and only that customer should route 8.8.8.8 to a specific gateway, add interface to the rule:
/routing/rule/add dst-address=8.8.8.8 action=lookup-only-in-table \
table=myTable interface=ether4
If the route in the table becomes inactive, for example when check-gateway finds the gateway down, the lookup fails and the destination becomes unreachable. In active-backup setups, allow traffic to fall back to the main table by changing the action from lookup-only-in-table to lookup.
Routing rules can also act as a basic firewall. For example, to block a customer on ether4 from accessing 192.168.1.0/24:
/routing/rule/add dst-address=192.168.1.0/24 interface=ether4 \
action=drop
Example: Send one device through a second WAN
The router has two internet connections: WAN1 on ether1, with the default route from its DHCP client, and WAN2 on ether2, with the gateway 10.20.0.1. All devices on the LAN, 192.168.88.0/24, use WAN1, except 192.168.88.50, which must use WAN2. Give the device a fixed address, so the rule keeps matching it (see Give a device a fixed address).
-
Create a routing table with a default route through WAN2. With
check-gateway=ping, the route becomes inactive when the gateway stops answering:/routing/table/add name=to-wan2 fib/ip/route/add dst-address=0.0.0.0/0 gateway=10.20.0.1 \routing-table=to-wan2 check-gateway=ping -
Send the traffic of the device to the table:
/routing/rule/add src-address=192.168.88.50/32 action=lookup \table=to-wan2With
action=lookup, the device falls back to WAN1 while the route through WAN2 is inactive. To keep it off WAN1, uselookup-only-in-table. -
Masquerade the traffic that leaves through WAN2, as for WAN1:
/ip/firewall/nat/add chain=srcnat out-interface=ether2 \action=masquerade
The rule sends all traffic of the device to the table, including traffic to other networks on the router, such as a guest LAN or the network of the WAN1 modem. To reach such a network directly, add a rule for it before the first rule:
/routing/rule/add dst-address=192.168.1.0/24 \
action=lookup-only-in-table table=main place-before=0
Connections the device opened before you added the rule can keep using WAN1 until they end. To check the path, run a traceroute on the device: the hop after the router is the WAN2 gateway.