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:
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.
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 220.127.116.11 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 18.104.22.168 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.|
|dst-address()||The destination address of the packet to match.|
|interface (string)||Incoming interface to match.|
|min-prefix (integer [0..4294967295])||Equivalent to Linux IP rule |
|routing-mark (string)||Match specific routing mark.|
|src-address (string)||The source address of the packet to match.|
|table (name)||Name of the routing table to use for lookup.|