Hairpin nat & 2 Vlans  [SOLVED]

If you installed RouterOS just now, and don't know where to start - ask here!
Scallop6312
just joined
Posts: 4
Joined: Mon May 13, 2024 8:35 pm

Hairpin nat & 2 Vlans

Post by Scallop6312 »

Hi,
I recently purchased a hAP ax3 and i'm currently rebuilding my homelab with Vlans thanks to RouterOS.

However, I face an issue that i'm not able to resolve.

My issue is that i'm unable to access my services inside my two vlans (or through the wireguard tunnel) using my domain name.
Outside of my network everything works as expected, I can reach my services using the domain name which point to the CNAME of my RouterOS DDNS domain.

I did look around for a few days and tried a few solutions but nothing seems to resolve my issue.
Here are some topics that I think are relevant to my situdation (that I tried the best I could without success):
viewtopic.php?t=190637
viewtopic.php?t=201099
viewtopic.php?t=177853
viewtopic.php?t=146370

I think that I do not need HairPin Nat because I have two different networks.

Can someone point me in the right direction ?


Here is my config export:

Code: Select all

/interface wireguard
add comment=back-to-home-vpn listen-port=26637 mtu=1420 name=back-to-home-vpn
/interface vlan
add interface=local name=Servers vlan-id=102
add interface=local name=Users vlan-id=100
/interface list
add comment=defconf name=WAN
add comment=defconf name=LAN
add name=listBridge
/ip pool
add name=dhcp_pool_Users ranges=192.168.100.2-192.168.100.126
/ip dhcp-server
add address-pool=dhcp_pool_Users interface=Users lease-time=8h name=\
    dhcp_Users
/interface bridge port
add bridge=local comment=defconf interface=ether2 pvid=102
add bridge=local comment=defconf interface=ether3 pvid=100
add bridge=local comment=defconf interface=ether4 pvid=100
add bridge=local comment=defconf interface=ether5 pvid=100
add bridge=local comment=defconf interface=wifi1 pvid=100
add bridge=local comment=defconf interface=wifi2 pvid=100
/ip neighbor discovery-settings
set discover-interface-list=LAN
/ipv6 settings
set disable-ipv6=yes
/interface bridge vlan
add bridge=local tagged=local untagged=ether2 vlan-ids=102
add bridge=local tagged=local untagged=ether3,ether4,ether5,wifi1,wifi2 \
    vlan-ids=100
/interface list member
add comment=defconf interface=local list=LAN
add comment=defconf interface=ether1 list=WAN
add interface=Users list=listBridge
add interface=Servers list=listBridge
add comment=defconf interface=Users list=LAN
add comment=defconf interface=Servers list=LAN
/ip address
add address=192.168.102.1/24 interface=Servers network=192.168.102.0
add address=192.168.100.1/25 interface=Users network=192.168.100.0
/ip cloud
set back-to-home-vpn=enabled ddns-enabled=yes
/ip dhcp-client
add comment=defconf dhcp-options=hostname,clientid,vendor-class-identifier \
    interface=ether1 use-peer-dns=no use-peer-ntp=no
/ip dhcp-server network
add address=192.168.100.0/25 dns-server=9.9.9.9,149.112.112.112 gateway=\
    192.168.100.1
/ip dns
set servers=9.9.9.9,149.112.112.112
/ip dns static
add address=192.168.88.1 comment=defconf name=router.lan
/ip firewall address-list
add address=192.168.102.0/24 list=lan_ips
add address=192.168.100.0/25 list=lan_ips
/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 log-prefix=NOTLAN
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=drop chain=forward comment="Inter Vlan filtering" \
    dst-address-list=lan_ips src-address-list=lan_ips
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 comment="defconf: masquerade" \
    ipsec-policy=out,none out-interface-list=WAN
add action=dst-nat chain=dstnat comment=WG dst-port=51820 in-interface-list=\
    WAN protocol=udp to-addresses=192.168.102.3 to-ports=51820
add action=dst-nat chain=dstnat comment="Revproxy http/tcp" dst-port=80 \
    in-interface-list=WAN protocol=tcp to-addresses=192.168.102.4 to-ports=80
add action=dst-nat chain=dstnat comment="Revproxy http/udp" dst-port=80 \
    in-interface-list=WAN protocol=udp to-addresses=192.168.102.4 to-ports=80
add action=dst-nat chain=dstnat comment="Revproxy http/tcp" dst-port=443 \
    in-interface-list=WAN protocol=tcp to-addresses=192.168.102.4 to-ports=\
    443
add action=dst-nat chain=dstnat comment="Revproxy http/tcp" dst-port=443 \
    in-interface-list=WAN protocol=udp to-addresses=192.168.102.4 to-ports=\
    443
/ip service
set telnet disabled=yes
set ftp disabled=yes
set www disabled=yes
set ssh disabled=yes
set api disabled=yes
set winbox address=192.168.100.0/25,192.168.102.0/24,192.168.216.2/32
set api-ssl disabled=yes
/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" \
    dst-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/Madrid
/system note
set show-at-login=no
/system ntp client
set enabled=yes
/system ntp client servers
add address=0.pool.ntp.org
add address=2.pool.ntp.org
add address=3.pool.ntp.org
add address=4.pool.ntp.org
/tool bandwidth-server
set enabled=no
/tool mac-server
set allowed-interface-list=none
/tool mac-server mac-winbox
set allowed-interface-list=LAN
Here is a very simple diagram describing my network :
mikrotik diag.png

Thank you very much!
You do not have the required permissions to view the files attached to this post.
erlinden
Forum Guru
Forum Guru
Posts: 2005
Joined: Wed Jun 12, 2013 11:59 am
Location: Netherlands

Re: Hairpin nat & 2 Vlans  [SOLVED]

Post by erlinden »

If the server is resolved (internally) with the public (DDNS) IP address, you need Hairpin NAT. Or have it resolved internally by it's private IP address (by adding the /ip dns static entry). For the latter you would have to have the MikroTik as DNS server (/ip dhcp-server network).
Scallop6312
just joined
Posts: 4
Joined: Mon May 13, 2024 8:35 pm

Re: Hairpin nat & 2 Vlans

Post by Scallop6312 »

Thank you ! Your were right and it seems to be fixed now I can access my services from Vlan 102 and from my VPN.
I used this post viewtopic.php?t=172380 to do the Hairpin Nat.

But now I cant access my services internally from my Vlan 100 using the domain as soon as I activate my "inter Vlan filtering rule".

Code: Select all

add action=drop chain=forward comment="Inter Vlan filtering" \
    dst-address-list=lan_ips src-address-list=lan_ips
Any idea ?

EDIT :
Changed the rule to this :

Code: Select all

add action=drop chain=forward comment="Inter Vlan filtering" connection-nat-state=!dstnat dst-address-list=lan_ips src-address-list=lan_ips
I seems to work, but I don't know if it's the proper way to do it.
Last edited by Scallop6312 on Tue May 14, 2024 8:13 am, edited 1 time in total.
erlinden
Forum Guru
Forum Guru
Posts: 2005
Joined: Wed Jun 12, 2013 11:59 am
Location: Netherlands

Re: Hairpin nat & 2 Vlans

Post by erlinden »

By looking at this rule...yes, I have.
What is the purpose of this rule?
Scallop6312
just joined
Posts: 4
Joined: Mon May 13, 2024 8:35 pm

Re: Hairpin nat & 2 Vlans

Post by Scallop6312 »

This rule is used to filter traffic between my L3 Vlans.
erlinden
Forum Guru
Forum Guru
Posts: 2005
Joined: Wed Jun 12, 2013 11:59 am
Location: Netherlands

Re: Hairpin nat & 2 Vlans

Post by erlinden »

You might want to consider to end the input and forward chain with a drop all rule. This way you are forced to make all accept rules explicit.

I think you want to block intervlan communication, you actually block all lan_ips to lan_ips
Scallop6312
just joined
Posts: 4
Joined: Mon May 13, 2024 8:35 pm

Re: Hairpin nat & 2 Vlans

Post by Scallop6312 »

erlinden wrote: Tue May 14, 2024 8:31 am You might want to consider to end the input and forward chain with a drop all rule. This way you are forced to make all accept rules explicit.
I don't really understand what you mean by ending the input and forward chain with a drop all rule.

And yes, I know that i'm blocking all lan_ips to lan_ips but it works because of the forward chain.
192.168.102.0/24 -> 192.168.100.0/25 is blocked by this rule
192.168.100.0/25 -> 192.168.100.2/24 is blocked by this rule
192.168.100.0/25 -> 192.168.100.0/25 is not blocked
192.168.102.0/24 -> 192.168.102.0/24 is not blocked
User avatar
anav
Forum Guru
Forum Guru
Posts: 19626
Joined: Sun Feb 18, 2018 10:28 pm
Location: Nova Scotia, Canada

Re: Hairpin nat & 2 Vlans

Post by anav »

Block all else means simply - keep default rules mostly, then only add needed traffic and all else is stopped cold.
Its clean, clear and efficient.

FORWARD CHAIN
{ default rules to keep }
add action=fasttrack-connection chain=forward comment="defconf: fasttrack" \
connection-mark=no-mark connection-state=established,related disabled=yes \
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
{ admin rules }
add action=accept chain=forward comment="internet traffic" in-interface-list=LAN out-interface-list=WAN
add action=accept chain=forward comment="port forwarding" connection-nat-state=dstnat

*********** Add any further ALLOW rules here ******************
add action=drop chain=forward comment="Drop all else"


Other rules could be
a. shared printer
b. Admin or management vlan to all other vlans
c. Server all need access to
d. wireguard access to all subnets

add action=accept chain=forward in-interface=back-to-home-vpn out-interface-list=LAN