Community discussions

MikroTik App
 
lanslot
just joined
Topic Author
Posts: 5
Joined: Tue Mar 05, 2024 4:36 pm

UPnP and Hairpin NAT

Tue Mar 05, 2024 4:53 pm

Hi there, I am having some trouble getting UPnP and Hairpin NAT to work together. Can you help?
  • NAT is working for my web server with src-nat rule #0 in the config and dst-nat rule #2
  • Hairpin NAT is also working for devices on the LAN to access the web server, with src-nat rule #1 and dst-nat rule #2
  • UPnP is working fine from the WAN with src-nat rule #0 and dynamic dst-nat rule #3
  • UPnP does not work for devices on the LAN to access the server, because the dynamic dst-nat rule requires in-interface=ether1 (ether1 is my WAN port). For traffic coming from LAN clients, the in-interface is the bridge, not ether1.

Questions:
  • Can I tell the UPnP service to generate dynamic rules without in-interface=ether1?
  • If not, what else can I do to make UPnP and Hairpin NAT to work together?

Here is my NAT config:
> ip firewall nat print
Flags: X - disabled, I - invalid; D - dynamic 
 0    ;;; defconf: src nat
      chain=srcnat action=src-nat to-addresses=<my wan ip>
      src-address=192.168.1.0/24 out-interface-list=WAN log=no log-prefix="" 
      ipsec-policy=out,none 

 1    ;;; defconf: hairpin nat
      chain=srcnat action=src-nat to-addresses=<my wan ip> 
      src-address=192.168.1.0/24 dst-address=192.168.1.0/24 
      out-interface-list=LAN log=no log-prefix="" 

 2    ;;; Web server
      chain=dstnat action=dst-nat to-addresses=192.168.1.2 protocol=tcp 
      dst-address-list=WanIP dst-port=80 log=no log-prefix="" 

 3  D ;;; upnp 192.168.1.3: TCP
      chain=dstnat action=dst-nat to-addresses=192.168.1.3 to-ports=53052 
      protocol=tcp dst-address=<my wan ip> in-interface=ether1 dst-port=53052 
 
TheCat12
Member Candidate
Member Candidate
Posts: 178
Joined: Fri Dec 31, 2021 9:13 pm

Re: UPnP and Hairpin NAT

Thu Mar 07, 2024 11:04 pm

Maybe change action=masquerade and set dst-address to be the one of the server on the hairpin nat rule as well as on the defconf rule unless there is a reason to be src-nat:
/ip firewall nat
set 0 action=masquerade src-address=""
set 1 action=masquerade dst-address=192.168.1.2
 
lanslot
just joined
Topic Author
Posts: 5
Joined: Tue Mar 05, 2024 4:36 pm

Re: UPnP and Hairpin NAT

Sat Mar 09, 2024 6:15 pm

Thanks for the suggestion. Unfortunately this didn't work. I think the problem is in the dst-nat rule #3, not in the src-nat rules.

Here is my understanding of the packet flow for Hairpin NAT (Scenario A):
  1. Say a LAN client at 192.168.1.100 sends a TCP SYN packet to my web server at <WAN_IP>:80
  2. The packet enters the router at the bridge interface.
  3. The router matches the packet with dst-nat rule #2 in my config. It performs the dst-nat action, rewriting the to-address to 192.168.1.2, which is the LAN IP of my web server.
  4. The router then matches the re-written packet with src-nat rule #1. It performs the src-nat action, rewriting the from-address to <WAN_IP> (configured as "to-address" in the src-nat rule)
  5. The packet is then sent to the web server, which sees from-address=<WAN_IP>, and to-address=<192.168.1.2>.
  6. The web server responds with a TCP SYN-ACK packet, with from-address=<192.168.1.2> and to-address=<WAN_IP>.
  7. The SYN-ACK packet is sent to the router. The router determines that the packet is part of a previous natted connection and undoes both the src-nat and dst-nat.
  8. The connection is established. Subsequent IP packets are processed at the router as part of the natted connection.

The problem with UPnP and Hairpin NAT is dst-nat rule #3 is not matched when the packet is originated from the LAN (Scenario B):
  1. Say a LAN client at 192.168.1.100 sends a TCP SYN packet to the UPnP server at <WAN_IP>:53052
  2. The packet enters the router at the bridge interface.
  3. The router does not find a matching dst-nat rule. The rule #3 is not matched because it requires in-interface=ether1 (the WAN interface), but this packet has in-interface=bridge.
  4. The router eventually forwards the request to the WAN interface with is bound to <WAN_IP>. However, there is no service listening at port 53052 on the router, so it sends back a TCP RST to the client.

Changing the src-nat rules to masquerade has a small effect on Scenario A => Step 4. Instead of rewriting from-address to <WAN-IP>, the masquerade action will rewrite from-address to 192.168.1.1 (the router's LAN IP), because the outgoing interface is the bridge interface, not the WAN interface. This doesn't matter much though, as both the WAN_IP and the bridge interface IP are routable to the router itself.

In Scenario B, it does not matter whether a src-nat action or a masquerade action is in place, because the dst-nat rule was never actioned in Step 3.


Is my understanding above correct?

Maybe change action=masquerade and set dst-address to be the one of the server on the hairpin nat rule as well as on the defconf rule unless there is a reason to be src-nat:
/ip firewall nat
set 0 action=masquerade src-address=""
set 1 action=masquerade dst-address=192.168.1.2
 
TheCat12
Member Candidate
Member Candidate
Posts: 178
Joined: Fri Dec 31, 2021 9:13 pm

Re: UPnP and Hairpin NAT

Sun Mar 10, 2024 10:35 pm

Is my understanding above correct?
Unfortunately, yes. And the only workaround I see is to make the addresses of the UPnP enabled devices static and add your dst-nat rules before the dynamic ones because, as you're probably familiar with, rules are processed in ascending order relative to their place in the list
 
lanslot
just joined
Topic Author
Posts: 5
Joined: Tue Mar 05, 2024 4:36 pm

Re: UPnP and Hairpin NAT

Mon Mar 11, 2024 12:32 am

Thanks for confirming! Do you by any chance know how to submit feature requests to MikroTik? It would be nice for them to add a config option to remove the in-interface=ether1 filter from dynamic dst-nat rules.
Unfortunately, yes. And the only workaround I see is to make the addresses of the UPnP enabled devices static and add your dst-nat rules before the dynamic ones because, as you're probably familiar with, rules are processed in ascending order relative to their place in the list
Last edited by lanslot on Mon Mar 11, 2024 3:14 pm, edited 1 time in total.
 
TheCat12
Member Candidate
Member Candidate
Posts: 178
Joined: Fri Dec 31, 2021 9:13 pm

Re: UPnP and Hairpin NAT

Mon Mar 11, 2024 7:29 am

 
tangent
Forum Guru
Forum Guru
Posts: 1404
Joined: Thu Jul 01, 2021 3:15 pm
Contact:

Re: UPnP and Hairpin NAT

Mon Mar 11, 2024 7:41 am

 
lanslot
just joined
Topic Author
Posts: 5
Joined: Tue Mar 05, 2024 4:36 pm

Re: UPnP and Hairpin NAT

Mon Mar 11, 2024 3:20 pm

I raised the issue in both places. Thank you both!

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], GoogleOther [Bot], ismel0x, K0NCTANT1N and 36 guests