Categories: MikroTik

Mikrotik RouterOS – Conditional DNS-forward

One of the big pains of having a complete network of L2TP/IPSec-devices is that you have to have DNS servers at each site, or run a central DNS-server which could be slow to reach and the users start complaining about slow internet.
This is a simple config-script to allow you to run your central DNS only on internal domains, and everything else on.. well, any DNS you want.

Notes:
The script creates a new pre-routing connection-mark, checking if the DNS-lookup on the router, port 53 is looking for something with “yourdomain.local”. When it is, it DST-NATs and forwards the packet to your internal DNS without doing anymore processing.

It assumes you use the router/gateway as DNS-server with remote requests enabled in DNS-setup.

# The domain you want to be able to look up
:global DOMAIN
yourdomain.local  

# This is needed because the script won't allow a variable and a dash.. so no "$DOMAIN-forward"
:global CONMARK yourdomain.local-forward

# The LAN ip of your router
:global ROUTER 192.168.0.1

# The DNS-server where you look up "yourdomain.local"
:global DNSSERVER
1.1.1.10

 

/ip firewall layer7-protocol add name=$DOMAIN regexp=$DOMAIN
/ip firewall mangle add chain=prerouting dst-address=$ROUTER layer7-protocol=$DOMAIN action=mark-connection new-connection-mark=$CONMARK protocol=tcp dst-port=53
/ip firewall mangle add chain=prerouting dst-address=$ROUTER
layer7-protocol=$DOMAIN action=mark-connection new-connection-mark=$CONMARK protocol=udp dst-port=53
/ip firewall nat add action=dst-nat chain=dstnat
connection-mark=$CONMARK to-addresses=$DNSSERVER
/ip firewall nat add
action=masquerade chain=srcnat connection-mark=$CONMARK

 

http://blog.degree.no/2013/10/mikrotik-routeros-conditional-dns-forward/

admin

Recent Posts

How to back up and restore the ESXi host configuration

Original https://kb.vmware.com/s/article/2042141 Backing up ESXi host configuration data To synchronize the configuration changed with persistent…

11 месяцев ago

vCenter Converter Standalone download

vCenter Converter Standalone 6.6.0 download (далее…)

12 месяцев ago

VMware remote console (VMRC) 12 download

VMware remote console (VMRC) 12.0.5 download Last version: VMware Remote Console 12.0.5  28 NOV 2023…

2 года ago

Mysql calculate row size for all tables

SELECT col_sizes.TABLE_SCHEMA, col_sizes.TABLE_NAME, SUM(col_sizes.col_size) AS EST_MAX_ROW_SIZE FROM ( SELECT cols.TABLE_SCHEMA, cols.TABLE_NAME, cols.COLUMN_NAME, CASE cols.DATA_TYPE WHEN…

2 года ago

Mysql change date format in database

UPDATE DB SET column = DATE(STR_TO_DATE(column, '%Y-%d-%m')) WHERE DATE(STR_TO_DATE(column, '%d-%m-%Y')) <> '0000-00-00'

2 года ago

esxi debian lvm extend partition

echo 1 > /sys/class/block/sda/device/rescan #maybe run under root> sudo fdisk /dev/sda2 > d > 2…

2 года ago