NAT Port Mapping question

I want to map an external port to a different port internally on a server in my network.

I understand how to make this work if I want to map to the same port. For example:

/ip firewall nat add chain=dstnat dst-port=1234 action=dst-nat protocol=tcp to-address=192.168.1.1 to-port=1234

will map the external 1234 port to the internal 1234 port on 192.168.1.1.

But I would like to do something like this:

/ip firewall nat add chain=dstnat dst-port=1234 action=dst-nat protocol=tcp to-address=192.168.1.1 to-port=80

and it does not work. I suspect it is because the traffic going back out to the outside client does not appear to be coming from the correct port.

I have masquarading enabled on my network for all other traffic, but masquarade does not allow me to specify a specific outbound port map. I need masquarading because my external IP address may change from time to time.

So, what do I need to map an external port to a different internal port for http traffic? I know this can be done on simple home routers, so I should be able to do it with RouterOS.

Thanks.

/ip firewall nat add chain=dstnat dst-port=1234 action=dst-nat protocol=tcp to-addresses=192.168.1.1 to-ports=80

This kind of NAT port mapping works for me as expected.
Does this NAT rule’s counter increments when you trying to connect from WAN side?
There is in your LAN server with IP 192.168.1.1 listening on port 80?
Check your settings and post your NAT configuration.

/ip firewall nat
export

Regards,

Yes, the rule increments, but the outside client does not connect. If I change the rule (and the inside device so that it listens on the same port number that the outside uses) then the outside client can connect. The problem only occurs if the inside and outside ports are different.

I’ve tried this with a number of different devices and ports, all with the same result.

The configuration is below. 192.168.1.1 was just illustrative, the exact one that is not working below is 192.168.88.30. The devices at .20 and .99 work, the device at .30 works if I connect from the inside to port 80 but does not work if I connect to 10123 from the outside.

 0   ;;; default configuration
     chain=srcnat action=masquerade to-addresses=0.0.0.0 out-interface=ether1-gateway 

 1   chain=dstnat action=dst-nat to-addresses=192.168.88.99 to-ports=443 protocol=tcp in-interface=ether1-gateway dst-port=443 

 2   chain=dstnat action=dst-nat to-addresses=192.168.88.20 to-ports=81 protocol=tcp in-interface=ether1-gateway dst-port=81 

 3   chain=dstnat action=dst-nat to-addresses=192.168.88.30 to-ports=80 protocol=tcp in-interface=ether1-gateway dst-port=10123 

 4 X chain=dstnat action=dst-nat to-addresses=192.168.89.246 to-ports=22 protocol=tcp in-interface=ether1-gateway dst-port=22 

 5   ;;; Loopback
     chain=srcnat action=accept

In your ds-nat rules there is no ‘dst-address’ (your WAN/public address) defined,
remove rule #5 commented as ‘Loopback’.
Do you have WWW service (port 80) enabled on your router?

/ip service export

If yes, disable it or move to diiferent port, eg. 8080.
What RouterOS version do you use?

Regards,

I removed the loopback rule.
I disabled the service port 80.

Neither of these changes made any difference. I’m running RouterOS 6.0RC2, but I had the same issue with 5.21.

It does not appear to have anthing to do with a particular port. If I use NAT to forward from an outside port to the same inside port #, it works. If I forward to a different inside port it does not work. Do I need some reverse mapping configured or a configuration in the firewall forward filter rules for a port other than the outside port, plus accept establised and accept related?

Post your firewall rules:

/ip firewall export

Regards,

Here is my firewall configuration:

# nov/06/2012 10:27:14 by RouterOS 6.0rc2
# software id = NHUG-DN89
#
/ip firewall connection tracking
set icmp-timeout=5s
/ip firewall filter
add action=jump chain=input comment="jump to chain ICMP" jump-target=ICMP protocol=icmp
add chain=input comment="default configuration - accept established" connection-state=established
add chain=input comment="default configuration - accept related" connection-state=related
add action=drop chain=input comment="Drop invalid" connection-state=invalid
add chain=input comment="allow PPTP tunnels to router" dst-port=1723 protocol=tcp
add action=drop chain=input comment="detect and drop port scan connections" protocol=tcp psd=21,3s,3,1
add action=tarpit chain=input comment="suppress DoS attack" connection-limit=3,32 protocol=tcp src-address-list=black_list
add action=add-src-to-address-list address-list=black_list address-list-timeout=1d chain=input comment="detect DoS attack" \
    connection-limit=10,32 protocol=tcp
add action=log chain=input comment="log dropped input packets" disabled=yes log-prefix=Filter:
add action=drop chain=input comment="default configuration - drop all else from WAN" in-interface=ether1-gateway
add chain=forward comment="For ISY via NAT" dst-port=443 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="Inbound port for Ubiquity Aircam" dst-port=81 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="Port for Foscam camera" dst-port=10123 in-interface=ether1-gateway protocol=tcp
add chain=forward dst-port=9009 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="chris computer" disabled=yes dst-port=22 protocol=tcp
add chain=forward comment="accept established" connection-state=established
add chain=forward comment="accept related" connection-state=related
add action=jump chain=forward comment="Jump to ICMP chain - Allow guest to ping LAN" jump-target=ICMP protocol=icmp
add action=drop chain=forward comment="To isolate the guest network" in-interface=bridge-guest out-interface=bridge-local
add action=drop chain=forward comment="Drop Invalid" connection-state=invalid
add action=drop chain=forward comment="drop all else from WAN" in-interface=ether1-gateway
add chain=ICMP comment="0:0 and limit for 5pac/s (echo reply)" icmp-options=0 limit=5,5 protocol=icmp
add chain=ICMP comment="3:3 and limit for 5pac/s (port unreachable)" icmp-options=3:3 limit=5,5 protocol=icmp
add chain=ICMP comment="3:4 and limit for 5pac/s (fragmentation needed)" icmp-options=3:4 limit=5,5 protocol=icmp
add chain=ICMP comment="8:0 and limit for 5pac/s (echo request)" icmp-options=8 limit=5,5 protocol=icmp
add chain=ICMP comment="11:0 and limit for 5pac/s (timeout exceeded)" icmp-options=11 limit=5,5 protocol=icmp
add action=drop chain=ICMP comment="Drop everything else" protocol=icmp
/ip firewall nat
add action=masquerade chain=srcnat comment="default configuration" out-interface=ether1-gateway to-addresses=0.0.0.0
add action=dst-nat chain=dstnat comment="ISY Remote access" dst-port=443 in-interface=ether1-gateway protocol=tcp to-addresses=\
    192.168.88.99 to-ports=443
add action=dst-nat chain=dstnat comment="Map port 81 to 81 on the Ubiquity Aircam" dst-port=81 in-interface=ether1-gateway protocol=\
    tcp to-addresses=192.168.88.20 to-ports=81
add action=dst-nat chain=dstnat dst-port=10123 in-interface=ether1-gateway protocol=tcp to-addresses=192.168.88.30 to-ports=80
add action=dst-nat chain=dstnat disabled=yes dst-port=554 in-interface=ether1-gateway protocol=tcp to-addresses=193.168.88.30 \
    to-ports=554
add action=dst-nat chain=dstnat dst-port=9009 in-interface=ether1-gateway protocol=tcp to-addresses=192.168.88.252 to-ports=9009
add action=dst-nat chain=dstnat comment="ssh for chris" disabled=yes dst-port=22 in-interface=ether1-gateway protocol=tcp \
    to-addresses=192.168.89.246 to-ports=22
add chain=srcnat comment=Loopback disabled=yes

Thanks for helping.

These rules are unnecessary:

add chain=forward comment="For ISY via NAT" dst-port=443 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="Inbound port for Ubiquity Aircam" dst-port=81 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="Port for Foscam camera" dst-port=10123 in-interface=ether1-gateway protocol=tcp
add chain=forward dst-port=9009 in-interface=ether1-gateway protocol=tcp
add chain=forward comment="chris computer" disabled=yes dst-port=22 protocol=tcp

According to packet flow diagram
http://wiki.mikrotik.com/wiki/Packet_Flow#Diagram
DST-NAT takes place before ‘input’ and ‘forward’ chains.

HTH,

Actually the rules are needed to let the traffic through the firewall after the port mapping. But your observation that the NAT takes place before the firewall forward chain is key to getting it to work. When I change the firewall rule to allow port 80 to forward (instead of 10123) I can access from outside! Success.

Thanks!

Sorry to resurrect this old thread, but I’m trying to do a similar thing and having no success.

I currently have working firewall/NAT rules to forward external port 5656 to internal port 5656 on 192.168.1.10. I’d like to also forward external port 21 to internal port 5656 on the same IP. The port forward works for 5656 but only partially works for 21, in that an initial connection is succeeding but then seemingly gets dropped.

The relevant NAT config is here:

add action=masquerade chain=srcnat out-interface=wan
add action=dst-nat chain=dstnat comment="server remote desktop" dst-port=5656 in-interface=wan protocol=tcp to-addresses=192.168.1.10 to-ports=5656
add action=dst-nat chain=dstnat comment="server rdp with ftp port" dst-port=21 in-interface=wan protocol=tcp to-addresses=192.168.1.10 to-ports=5656

Immediate thoughts - Check that you aren’t running port 21 on the router itself (IP Services) and check that you don’t actually need ports 21 & 20 for your application.

Also remember that such NATed traffic also needs to be allowed in the forwarding chain filters.

Thanks for the heads up on the IP services, FTP was indeed running. I disabled it but the problem persists.

My usual method of setting up forwards is to create both the NAT rule as well as the firewall rule to accept the connection, which works fine. I believe all I should need to do in order to have two external ports forward to a single internal port is to create the additional NAT entry, but no matching firewall rule is needed since it’s all hitting the same internal port. Is that correct?

If you are trying to forward two different outside ports to the same inside port on the same inside ip that is an overlap and I don’t believe that will work. I have not met a firewall device yet that allows you to do that.

-Rich

Turns out it was my ISP dropping the traffic. Guess FTP is too abused these days :slight_smile: