Mikrotik PBR Guide
Mikrotik PBR Guide
Mikrotik PBR Guide
Deploying CACHEBox with a Mikrotik Router in Policy Based Routing (PBR) mode
Requirements:
• Perform initial setup of your CACHEBox, so that it has an IP address on the eth0 interface, with the default route set to
the Cachebox-facing interface on the Mikrotik router.
For example:
Mikrotik ether3: 192.168.2.1/24
CACHEBox eth0: 192.168.2.2/24
CACHEBox Default Route: 192.168.2.1
1. If your Cachebox is within a private IP range, you may need to create a new NAT rule on the Mikrotik, to NAT the
outgoing requests to the Internet, coming from the Cachebox's Private IP address.
This can be done by navigating to the IP > Firewall > NAT tab in the Mikrotik interface and creating a NAT rule with the
following options:
Chain: src-nat
[Action Tab]
Action: masquerade
2. Navigate to the IP > Firewall > Mangle page and create a new Mangle rule with the following options:
Chain: Prerouting
Protocol: tcp
Dst-Port: 80
[Action Tab]
Passthrough: Ticked/Enabled
3. Navigate to the IP > Routes page, and create a new route with the following options:
Dst-Address: 0.0.0.0/0
4. On the CACHEBox web interface, navigate to Cache > Mode and set Deployment type to Gateway Interception (PBR).
• Client HTTP traffic should now be getting re-directed to the Cachebox. This can be verified by going to the Cache
Overview page in the Cachebox.
• You should also see the packet counters incrementing under the Cachebox Mangle rule in the Mikrotik web interface.
If your client machines have Public IP addresses, or you want to preserve the original client's IP address to upstream equipment
– you will need to follow this Source Address Spoofing guide:
1. Navigate to the IP > Firewall > Mangle page and create a new Mangle rule with the following options:
Chain: Prerouting
Protocol: tcp
Any-Port: 80
[Action Tab]
Passthrough: Ticked/Enabled
Chain: Prerouting
Protocol: tcp
[Action Tab]
Passthrough: Ticked/Enabled
3. Navigate to the IP > Routes page, and create a new route with the following options:
Dst-Address: 0.0.0.0/0
4. On the CACHEBox web interface, set the deployment mode to Gateway Interception (PBR) in the Cache > Mode page.
Then set navigate to Cache > Advanced and set Source Address Spoofing to 'Enabled'.
5. You should then see successful requests appear in the Cache Overview page on the CACHEBox web interface.
Appendix A: Implementing ToS/DSCP Marking (Squid ZPH marking) for Cache HIT packets
If you implement Queues or Rate-limiting in your Mikrotik router, to restrict bandwidth to your clients – you can allow full LAN
speed to your Cache HIT packets by implementing ToS/DSCP marking as follows:
1. In the CACHEBox web interface, navigate to the Cache > Custom page, and enter the following configuration:
zph_mode tos
zph_local 0x30
qos_flows local-hit=0x30
(To check which version of Squid you are running, you may navigate to the System > Services page, and check the version displayed on the
line starting 'Web Cache'.)
The 0x30 value is arbitrary, we will use this value when entering the Mikrotik configuration later on.
You can see the following web page for guidance on different values.
http://www.tucny.com/Home/dscp-tos
2. In the Mikrotik router, navigate to the IP > Firewall > Mangle page and create a new mangle rule with the following
configuration:
[General tab]
Chain: Prerouting
[Advanced Tab]
DSCP (TOS): 12
(Mikrotik can only match on DSCP decimal values, not ToS Hex values –
and so this value is the converted result of ToS 0x30, which equals a DSCP
decimal value of 12). You may use the following page to see the converted
DSCP decimal value of your chosen ToS Hex mark that was used in the
Cache Custom Config:
http://www.tucny.com/Home/dscp-tos
[Action Tab]
Passthrough: Ticked/Enabled
3. In the Mikrotik, navigate to the Queues menu and follow the following steps, depending on whether you are using only Simple
Queues or more advanced Queue Trees.
[General Tab]
Target: 0.0.0.0/0
[Advanced Tab]
Packet Marks: cacheboxdscp (or the packet mark you created in step 1)
Ensure that this new Queue is moved to the top of the Queue list,
so that it is processed before the other queues.
If you are already only using Advanced Queues (Queue Trees), then create a new Queue Tree with the following configuration:
Parent: global
Limit at: 0
Max Limit: 0
Burst Limit: 0
Burst Threshold: 0
Ensure that this new Queue is moved to the top of the Queue
list, so that it is processed before the other queues.