Policy routing is the method to steer traffic matching certain criteria to a certain gateway. This can be used to force some customers or specific protocols from the servers (for example HTTP traffic) to always be routed to a certain gateway. It can even be used to steer local and overseas traffic to different gateways.
RouterOS implements several components that can be used to achieve said task:
- routing tables
- routing rules
- firewall mangle marking
A router can have multiple routing tables with its own set of routes routing the same destination to different gateways.
Tables can be seen and configured from the
By default, RouterOS has only the ‘main‘ routing table:
[admin@rack1_b33_CCR1036] /routing/table> print
Flags: D - dynamic; X - disabled, I - invalid; U - used
0 D name="main" fib
If a custom routing table is required, it should be defined in this menu prior to using it anywhere in the configuration.
Let’s consider a basic example where we have two gateways 172.16.1.1 and 172.16.2.1 and we want to resolve 126.96.36.199 only in the routing table named ‘myTable‘ to the gateway 172.16.2.1:
For a user-created table to be able to resolve the destination, the main routing table should be able to resolve the destination too.
In our example, the main routing table should also have a route to destination 188.8.131.52 or at least a default route, since the default route is dynamically added by the DHCP for safety reasons it is better to add 184.108.40.206 also in the main table.
[admin@rack1_b33_CCR1036] /ip/route> print detail Flags: D - dynamic; X - disabled, I - inactive, A - active;c - connect, s - static, r - rip, b - bgp, o - ospf, d - dhcp, v - vpn, m - modem, y - copy;
H - hw-offloaded; + - ecmp
DAd dst-address=0.0.0.0/0 routing-table=main pref-src="" gateway=172.16.1.1
immediate-gw=172.16.1.1%ether8 distance=1 scope=30 target-scope=10
0 As dst-address=220.127.116.11/32 routing-table=main pref-src="" gateway=172.16.1.1
immediate-gw=172.16.1.1%ether8 distance=1 scope=30 target-scope=10 suppress-hw-offload=no
DAc dst-address=172.16.1.0/24 routing-table=main gateway=ether8 immediate-gw=ether8
distance=0 scope=10 suppress-hw-offload=no local-address=172.16.1.2%ether8
DAc dst-address=172.16.2.0/24 routing-table=main gateway=ether7 immediate-gw=ether7
distance=0 scope=10 suppress-hw-offload=no local-address=172.16.2.2%ether7
1 As dst-address=18.104.22.168/32 routing-table=myTable pref-src="" gateway=172.16.2.1
immediate-gw=172.16.2.1%ether7 distance=1 scope=30 target-scope=10 suppress-hw-offload=no
But configuration above is not enough, we need a method to force the traffic to actually use our newly created table. RouterOS gives you two options to choose from:
- firewall mangle – it gives more control over the criteria to be used to steer traffic, for example, per connection or per packet balancing, etc. For more info on how to use mangle marking see Firewall Marking examples.
- routing rules – a basic set of parameters that can be used to quickly steer traffic. This is the method we are going to use for our example.
Routing rules allow steering traffic based on basic parameters like a source address, a destination address, or in-interface as well as other parameters.
For our example, we want to select traffic with destination 22.214.171.124 and do not fall back to the main table:
Lets’s say that we know that customer is connected to ether4 and we want only that customer to route 126.96.36.199 to a specific gateway. We can use the following rule:
If for some reason the gateway used in our table goes down, the whole lockup will fail and the destination will not be reachable. In active-backup setups we want the traffic to be able to fall back to the main table. To do that change the action from
Also, routing rules can be used as a very “basic firewall”. Let’s say we do not want to allow a customer connected to ether4 to be able to access the 192.168.1.0/24 network:
List of all the parameters that can be used by routing rules:
|action (drop | lookup | lookup-only-in-table | unreachable)
|An action to take on the matching packet:drop – silently drop the packet.lookup – perform a lookup in routing tables.lookup-only-in-table – perform lookup only in the specified routing table (see table parameter).unreachable – generate ICMP unreachable message and send it back to the source.
|disabled (yes | no)
|The disabled rule is not used.
|The destination address of the packet to match.
|Incoming interface to match.
|min-prefix (integer [0..4294967295])
|Equivalent to Linux IP rule
suppress_prefixlength . For example to suppress the default route in the routing decision set the value to 0.
|Match specific routing mark.
|The source address of the packet to match.
|Name of the routing table to use for lookup.