If I have a static route on my server to the external subnet, things work. But not if the route is removed. So I know the source is not being translated.
128.x.x.[50-70] are the external clients.
128.x.x.4 is the external interface IP
172.x.x.199 is the internal server
172.x.x.123 is the internal firewall address that traffic should go back to.
172.x.x.123 has no idea of the routes to 128.x.x.x
At this point packet has src-address=128.x.x.50 and dst-address=172.x.x.199
3. whatever firewall does, possibly allows this packet
4. ```text
add action=src-nat chain=srcnat src-address=128.x.x.50 dst-address=172.x.x.199 <other settings> to-addresses=172.x.x.123
at this point packet has src-address=172.x.x.123 and dst-address=172.x.x.199
You have to adjust the NAT rules to cover all needed WAN addresses (eitger using address range or address subnet mask) …
You do not need to match on src-address. It is sufficient to match on protocol, port and dst-address.
So you do the dst-nat based on the protocol and port and with dst-address equal to the local address of the router, translating the dst-addr to the address of the device you want to access.
Then, also have a src-nat that sets the source address equal to the router address for such traffic towards the dst-addr of the device (and the protocol and port).
Do not apply a too-wide match (e.g. omitting the protocol and port) because you may lock yourself out of the router!
Ok but note that due to the way you wrote the src-nat, ALL traffic towards that address will be src-natted not only the traffic you dst-natted to that server.
This probably does not matter when your server has no routes at all, but when it would have routes to other local subnets it would cause problems.
In that case you should put the “protocol=tcp dst-port=5001” in the src-nat rule as well.
I use this method all the time to recover devices at remote sites that have fallen back to default settings.
E.g. on a radio link between MikroTik routers made using Ubiquiti WiFi radios that sometimes revert to their factory default IP of 192.168.1.20 with no routes.
Temporarily add 192.168.1.1/24 on the port it is connected to, add those two rules for tcp/443 and I can connect the device and reconfigure it.
Very handy!
I took source addresses from your post #5 above … and used those as illustration how to make NAT rules as speciffic as possible not to affect the rest of traffic.
With NAT rules there are two kinds of parameters:
selection criteria, which define which packets will be affected by particular rule.
Almost all parameters fall into this category, but most used are src-address, dst-address, src-address-list, dst-address-list, protocol, src-port, dst-port, in-interface, out-interface. Not all of these parameters have to be set, but one has to be careful to set selection parameters so that the rule is not “too greedy”.
action parameters, which define what needs to be changed on packets.
These are the following two parameters: to-addresses and to-ports and define the resulting value. With src-nat the corresponding src-* values get overwritten and with dst-nat the corresponding dst-* values get overwritten. If one of the two parameters is not set (most frequently to-ports is not set), then the corresponding src-* or dst-* value is not changed.
It is perfectly fine to use e.g. src-address as selection criterion in src-nat with to-addresses set … just keep in mind that first selection rules are ran against original packet[] and action is done on matching packets.
[] I used “original” meaning the packet which is being matched … which might got changed already before this NAT rule is being evaluated.
Think of LAN (172.x.x.0/24) and WAN (the rest). And devices in LAN don’t have any route defined … hence they can only communicate with devices within same L2 domain (same subnet).
The executive engine below is the very same netfilter module. The Mikrotik’s configuration front-end is slightly different from iptables but the structure of the rules (chains, tables) is exactly the same.
As compared to iptables:
you have the interface lists and address lists (which are extensions to the basic netfilter),
you cannot change the default handling for the chain (it is always “accept”, so if you want to change this to “drop”, you have to add a “drop” rule as the last one in the chain).
you cannot configure dst-nat in output chain (grrr).
The fact that srcnat chain cannot refer to in-interface (and dst-nat cannot refer to out-interface) is just a feature of netfilter so no front-end cannot do anything about it. You can work this around by assigning a connection-mark to the connection in prerouting chain of mangle table (where the in-interface can be referred to) and then refer to that connection-mark in src-nat rule in the srcnat chain of the nat table.