Community discussions

MikroTik App
 
mktbm
just joined
Topic Author
Posts: 2
Joined: Tue Apr 09, 2024 11:46 pm

Firewall NAT for DNS traffic not working

Sun Apr 14, 2024 12:51 pm

This is my first time posting on the Mikrotik forum so please let me know if any information is missing.
 
Network setup
My network uses a Mikrotik hAP ax3 as a router connected to my ISP modem (192.168.1.1). A local DNS server is running with an assigned IP-address of 10.0.0.10.
The router serves as a DHCP server for all clients with an IP-pool in the range of 10.0.0.100 to 10.0.0.254 and a DNS server of 10.0.0.1 (router address).
The Mikrotik DNS server is configured as the ISP modem address, making use of the ISP provided DNS servers by default.

network.png

Problem description
I want to make sure all DNS traffic is redirected to the local DNS server using a firewall NAT rule for TCP/UDP traffic on port 53.

I have configured the following firewall NAT rules:
add action=dst-nat chain=dstnat comment="DNS dst-nat UDP" disabled=no \
    dst-port=53 in-interface=bridge-LAN protocol=udp src-address=!10.0.0.10 \
    to-addresses=10.0.0.10 to-ports=53
add action=dst-nat chain=dstnat comment="DNS dst-nat TCP" disabled=no \
    dst-port=53 in-interface=bridge-LAN protocol=tcp src-address=!10.0.0.10 \
    to-addresses=10.0.0.10 to-ports=53

The following behaviour is observed:
  • When configuring the DHCP server to provide the DNS address of the Mikrotik router (10.0.0.1), everything works as expected.
  • When configuring the DHCP server to provide the DNS address of my local DNS server (10.0.0.10), everything works as expected as well.
  • However, if I configure the DHCP server to provide the DNS address of the router and enable the NAT rules then DNS queries are not resolved anymore.

Any help would be much appreciated.

Configuration
RouterOS version is 7.14.2 with RouterBOARD version 7.14.2 using the following configuration:

# 2024-04-14 10:37:19 by RouterOS 7.14.2
# software id = 2IEW-LXGZ
#
# model = C53UiG+5HPaxD2HPaxD
# serial number = HEQ09CE89JG
/interface bridge
add admin-mac=78:9A:18:05:DA:B5 auto-mac=no name=bridge-LAN port-cost-mode=\
    short
/interface ethernet
set [ find default-name=ether1 ] name=ether1-LAN1
set [ find default-name=ether2 ] name=ether2-LAN2
set [ find default-name=ether3 ] name=ether3-LAN3
set [ find default-name=ether4 ] name=ether4-LAN4
set [ find default-name=ether5 ] name=ether5-WAN
/interface list
add comment=defconf name=WAN
add comment=defconf name=LAN
/interface wifi channel
add band=2ghz-ax disabled=no name=channel-2.4GHz skip-dfs-channels=10min-cac
add band=5ghz-ax disabled=no name=channel-5GHz skip-dfs-channels=10min-cac
/interface wifi security
add authentication-types=wpa2-psk,wpa3-psk disabled=no name=sec-home
/interface wifi configuration
add country=Netherlands disabled=no mode=ap name=cfg-home security=sec-home \
    ssid=wifi-test
/interface wifi
set [ find default-name=wifi2 ] channel=channel-2.4GHz configuration=cfg-home \
    configuration.mode=ap disabled=no name="wifi1-WLAN1 (2.4GHz)" \
    security.authentication-types=wpa2-psk,wpa3-psk
set [ find default-name=wifi1 ] channel=channel-5GHz configuration=cfg-home \
    configuration.mode=ap disabled=no name="wifi2-WLAN2 (5GHz)" \
    security.authentication-types=wpa2-psk,wpa3-psk
/ip pool
add name=dhcp-pool ranges=10.0.0.100-10.0.0.254
/ip dhcp-server
add address-pool=dhcp-pool interface=bridge-LAN lease-time=10m name=\
    dhcp-server
/ip smb users
set [ find default=yes ] disabled=yes
/interface bridge port
add bridge=bridge-LAN interface=ether1-LAN1 internal-path-cost=10 path-cost=\
    10
add bridge=bridge-LAN interface=ether2-LAN2 internal-path-cost=10 path-cost=\
    10
add bridge=bridge-LAN interface=ether3-LAN3 internal-path-cost=10 path-cost=\
    10
add bridge=bridge-LAN interface=ether4-LAN4 internal-path-cost=10 path-cost=\
    10
add bridge=bridge-LAN interface="wifi2-WLAN2 (5GHz)" internal-path-cost=10 \
    path-cost=10
add bridge=bridge-LAN interface="wifi1-WLAN1 (2.4GHz)" internal-path-cost=10 \
    path-cost=10
/ip firewall connection tracking
set udp-timeout=10s
/ip neighbor discovery-settings
set discover-interface-list=LAN
/interface detect-internet
set detect-interface-list=WAN
/interface list member
add interface=bridge-LAN list=LAN
add interface=ether5-WAN list=WAN
/ip address
add address=10.0.0.1/24 comment=defconf interface=bridge-LAN network=10.0.0.0
/ip dhcp-client
add interface=ether5-WAN
/ip dhcp-server lease
add address=10.0.0.10 comment="Static IP-address for DNS server" mac-address=\
    B8:27:EB:4F:FF:D0 server=dhcp-server
/ip dhcp-server network
add address=10.0.0.0/24 dns-server=10.0.0.1 gateway=10.0.0.1
#add address=10.0.0.0/24 dns-server=10.0.0.10 gateway=10.0.0.1
/ip dns
set allow-remote-requests=yes
/ip firewall filter
add action=accept chain=input comment=\
    "defconf: accept established,related,untracked" connection-state=\
    established,related,untracked
add action=drop chain=input comment="defconf: drop invalid" connection-state=\
    invalid
add action=accept chain=input comment="defconf: accept ICMP" protocol=icmp
add action=accept chain=input comment=\
    "defconf: accept to local loopback (for CAPsMAN)" dst-address=127.0.0.1
add action=drop chain=input comment="defconf: drop all not coming from LAN" \
    in-interface-list=!LAN
add action=accept chain=forward comment="defconf: accept in ipsec policy" \
    ipsec-policy=in,ipsec
add action=accept chain=forward comment="defconf: accept out ipsec policy" \
    ipsec-policy=out,ipsec
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" \
    connection-state=established,related hw-offload=yes
add action=accept chain=forward comment=\
    "defconf: accept established,related, untracked" connection-state=\
    established,related,untracked
add action=drop chain=forward comment="defconf: drop invalid" \
    connection-state=invalid
add action=drop chain=forward comment=\
    "defconf: drop all from WAN not DSTNATed" connection-nat-state=!dstnat \
    connection-state=new in-interface-list=WAN
/ip firewall nat
add action=masquerade chain=srcnat ipsec-policy=out,none out-interface-list=\
    WAN
add action=dst-nat chain=dstnat comment="DNS dst-nat UDP" disabled=no \
    dst-port=53 in-interface=bridge-LAN protocol=udp src-address=!10.0.0.10 \
    to-addresses=10.0.0.10 to-ports=53
add action=dst-nat chain=dstnat comment="DNS dst-nat TCP" disabled=no \
    dst-port=53 in-interface=bridge-LAN protocol=tcp src-address=!10.0.0.10 \
    to-addresses=10.0.0.10 to-ports=53
/ip service
set telnet disabled=yes
set ftp disabled=yes
set ssh disabled=yes
set api disabled=yes
set api-ssl disabled=yes
/ip smb shares
set [ find default=yes ] directory=/pub
/ipv6 firewall address-list
add address=::/128 comment="defconf: unspecified address" list=bad_ipv6
add address=::1/128 comment="defconf: lo" list=bad_ipv6
add address=fec0::/10 comment="defconf: site-local" list=bad_ipv6
add address=::ffff:0.0.0.0/96 comment="defconf: ipv4-mapped" list=bad_ipv6
add address=::/96 comment="defconf: ipv4 compat" list=bad_ipv6
add address=100::/64 comment="defconf: discard only " list=bad_ipv6
add address=2001:db8::/32 comment="defconf: documentation" list=bad_ipv6
add address=2001:10::/28 comment="defconf: ORCHID" list=bad_ipv6
add address=3ffe::/16 comment="defconf: 6bone" list=bad_ipv6
/ipv6 firewall filter
add action=accept chain=input comment=\
    "defconf: accept established,related,untracked" connection-state=\
    established,related,untracked
add action=drop chain=input comment="defconf: drop invalid" connection-state=\
    invalid
add action=accept chain=input comment="defconf: accept ICMPv6" protocol=\
    icmpv6
add action=accept chain=input comment="defconf: accept UDP traceroute" port=\
    33434-33534 protocol=udp
add action=accept chain=input comment=\
    "defconf: accept DHCPv6-Client prefix delegation." dst-port=546 protocol=\
    udp src-address=fe80::/10
add action=accept chain=input comment="defconf: accept IKE" dst-port=500,4500 \
    protocol=udp
add action=accept chain=input comment="defconf: accept ipsec AH" protocol=\
    ipsec-ah
add action=accept chain=input comment="defconf: accept ipsec ESP" protocol=\
    ipsec-esp
add action=accept chain=input comment=\
    "defconf: accept all that matches ipsec policy" ipsec-policy=in,ipsec
add action=drop chain=input comment=\
    "defconf: drop everything else not coming from LAN" in-interface-list=\
    !LAN
add action=accept chain=forward comment=\
    "defconf: accept established,related,untracked" connection-state=\
    established,related,untracked
add action=drop chain=forward comment="defconf: drop invalid" \
    connection-state=invalid
add action=drop chain=forward comment=\
    "defconf: drop packets with bad src ipv6" src-address-list=bad_ipv6
add action=drop chain=forward comment=\
    "defconf: drop packets with bad dst ipv6" dst-address-list=bad_ipv6
add action=drop chain=forward comment="defconf: rfc4890 drop hop-limit=1" \
    hop-limit=equal:1 protocol=icmpv6
add action=accept chain=forward comment="defconf: accept ICMPv6" protocol=\
    icmpv6
add action=accept chain=forward comment="defconf: accept HIP" protocol=139
add action=accept chain=forward comment="defconf: accept IKE" dst-port=\
    500,4500 protocol=udp
add action=accept chain=forward comment="defconf: accept ipsec AH" protocol=\
    ipsec-ah
add action=accept chain=forward comment="defconf: accept ipsec ESP" protocol=\
    ipsec-esp
add action=accept chain=forward comment=\
    "defconf: accept all that matches ipsec policy" ipsec-policy=in,ipsec
add action=drop chain=forward comment=\
    "defconf: drop everything else not coming from LAN" in-interface-list=\
    !LAN
/system clock
set time-zone-name=Europe/Amsterdam
/system identity
set name=router
/system note
set show-at-login=no
/system ntp client
set enabled=yes
/system ntp client servers
add address=0.nl.pool.ntp.org
add address=1.nl.pool.ntp.org
add address=2.nl.pool.ntp.org
add address=3.nl.pool.ntp.org
/tool mac-server
set allowed-interface-list=LAN
/tool mac-server mac-winbox
set allowed-interface-list=LAN
You do not have the required permissions to view the files attached to this post.
 
User avatar
vingjfg
Member
Member
Posts: 332
Joined: Fri Oct 20, 2023 1:45 pm

Re: Firewall NAT for DNS traffic not working

Sun Apr 14, 2024 1:40 pm

Given that the server and the clients are on the same network, the initial packet goes through the router and is dst-natted to 10.0.0.10, but the response goes directly from the server 10.0.0.10 to the client, which expected a reply from 10.0.0.1. The client drops that datagram.

If you *really* want to work that way, look into hairpin NAT.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 19500
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Firewall NAT for DNS traffic not working  [SOLVED]

Sun Apr 14, 2024 4:14 pm

First, be accurate!
You no NOT have an ISP modem. It is an ISP modem/router if it its giving you a private IP address on the WAN side.

If you want to as much as possible direct traffic to your own DNS server, great, but keep in mind any browser using VPn proxy or whatever it is they use, can bypass that.
So effectiveness all depends upon the knowledge level of your users

Setup points, this is the only entry you need here you have two one in red.
/ip dhcp-server network
add address=10.0.0.0/24 comment=defconf dns-server=10.0.0.10 gateway=\
10.0.0.1


Your DST-NAT rules are fine, I tend to use firewall address list called excluded, in case I want to add any other addresses, besides the server itself,that should not be forced to the DNS server. (src-address-list=!excluded)
You still need access to router DNS for the DNS server itself.

Only other things I would modify slightly
/ip dns
set allow-remote-requests=yes servers=9.9.9.9
{ ensure what server you put here is separate from the ones your own server is using! }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Try these changes first....................
If still not working please detail what doesn't work.

Then next add this hairpin nat rule.
/ip firewall nat
add chain=srcnat action=masquerade src-address=10.0.0.0/24 dst-address=10.0.0.0/24
 
mktbm
just joined
Topic Author
Posts: 2
Joined: Tue Apr 09, 2024 11:46 pm

Re: Firewall NAT for DNS traffic not working

Mon Apr 15, 2024 10:06 pm

Thank you for the help, adding the following rule solved the issue I was having.

/ip firewall nat
add action=masquerade chain=srcnat comment="hairpin NAT" dst-address=10.0.0.0/24 src-address=10.0.0.0/24

Who is online

Users browsing this forum: Fogga, mikeyt, taramid and 21 guests