Community discussions

MikroTik App
 
ivugrinec
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 82
Joined: Thu Feb 17, 2011 11:43 am

DST NAT return from same IP

Wed May 23, 2018 12:14 pm

Hi,

I need a little help. I have a Mikrotik CCR router with interface that has several public IP's. I also have DST-NAT rules (port forward) where i use public IP's and port forward (DST-NAT) external ports to internal server. I need to achieve that a return packet is sent with SRC IP = public IP that packet came in first place.
https://imgur.com/a/YAAyNTw

As you can see in the Wireshark example i have packet comming from 31.217.20.96 and it is sent to 194.152.192.156. Return packet is sent through default gateway and not with 194.152.192.156 as SRC IP.

Any advice?
 
User avatar
CZFan
Forum Guru
Forum Guru
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: DST NAT return from same IP

Wed May 23, 2018 2:17 pm

You need to mark the connection / routing coming in on that interface in Mangle

Then create a route out using the routing mark, e.g.:
/ip firewall mangle
add chain=prerouting in-interface=WAN1 connection-state=new passthrough=yes connection-mark=no-mark action=mark-connection new-connection-mark=to-WAN1
add chain=prerouting connection-mark=to-WAN1 passthrough=no action=mark-routing new-routing-mark=to-WAN1
/ip route
add distance=2 dst-address=0.0.0.0/0 gateway=<IP of GW> routing-mark=to-WAN1
 
ivugrinec
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 82
Joined: Thu Feb 17, 2011 11:43 am

Re: DST NAT return from same IP

Wed May 23, 2018 2:57 pm

First, thank you for trying to help, but i don't think suggested solution will work for me. Let me try to describe the situation:
I have CCR router with several public IP's (one subnet). That several public IP's all have the same gateway.
When some client sends packet to my network, ho send's it to 194.152.192.156:16666. This is forwarded (DST-NAT) to 10.10.11.X:9002. I need this server to forward packet's back to router and back to client. The local server 10.10.11.x now uses default gateway and sends packet back to client with 194.152.192.154 as source IP and not 194.152.192.156 as the client expects.
Both Public IP's have the same gateway.
 
User avatar
CZFan
Forum Guru
Forum Guru
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: DST NAT return from same IP

Wed May 23, 2018 8:42 pm

Is there a static port number the server replies on? If so, then try above your normal NAT / Masquerade rule:
ip firewall nat add chain=srcnat src-address=10.10.11.X dst-port(or src-port)=9002 action=src-nat to-addresses=194.152.192.156
or maybe a one to one NAT:
ip firewall nat add chain=dstnat dst-address=194.152.192.156 action=dst-nat to-addresses=10.10.11.X
ip firewall nat add chain=srcnat src-address=10.10.11.X action=src-nat to-addresses=194.152.192.156
 
Sob
Forum Guru
Forum Guru
Posts: 9121
Joined: Mon Apr 20, 2009 9:11 pm

Re: DST NAT return from same IP

Fri May 25, 2018 4:12 am

If the return packet is direct reply, as it seems to be, conntrack should take care of this automatically. There must be something unusual what is not apparent from provided description. Maybe a config export would help...
 
manelfl
newbie
Posts: 30
Joined: Mon May 18, 2015 12:55 pm

Re: DST NAT return from same IP

Fri May 25, 2018 11:28 am

I agree with CZFan reply.

If you mark connection (connection tracking have to be enable: ip firewall connection tracking set enabled=yes), all packets in this connection will be marked.
With this connection mark you mark packet with routing mark.
With this routing mark, you say to mikrotik which wan connection use.

Packet from 10.10.11.x is not new, its is a reponse to dst-nat. So it is marked with connection mark and with routing mark. So it will be send using the right mikrotik interface.

First, thank you for trying to help, but i don't think suggested solution will work for me. Let me try to describe the situation:
I have CCR router with several public IP's (one subnet). That several public IP's all have the same gateway.
When some client sends packet to my network, ho send's it to 194.152.192.156:16666. This is forwarded (DST-NAT) to 10.10.11.X:9002. I need this server to forward packet's back to router and back to client. The local server 10.10.11.x now uses default gateway and sends packet back to client with 194.152.192.154 as source IP and not 194.152.192.156 as the client expects.
Both Public IP's have the same gateway.
 
User avatar
CZFan
Forum Guru
Forum Guru
Posts: 2098
Joined: Sun Oct 09, 2016 8:25 pm
Location: South Africa, Krugersdorp (Home town of Brad Binder)
Contact:

Re: DST NAT return from same IP

Fri May 25, 2018 1:30 pm

If the return packet is direct reply, as it seems to be, conntrack should take care of this automatically. There must be something unusual what is not apparent from provided description. Maybe a config export would help...

That also went through my mind, but I am not that knowledgeable on Mikrotik yet and thought maybe Mikrotik does not work like that.
 
Sob
Forum Guru
Forum Guru
Posts: 9121
Joined: Mon Apr 20, 2009 9:11 pm

Re: DST NAT return from same IP

Fri May 25, 2018 2:45 pm

It's basic functionality. If packet is reply for connection established from outside (even though it's not true connection with udp, but conntrack see it as such), then it will get the right source address (same as outside client used as destination) automatically.

Connection and route marking is required for multiple WANs, and it's only for choosing the right path, addresses will be correct even without it (it's just that the right address sent the wrong way can't succeed). But from the description it doesn't sound as two WANs, only one with more addresses.
 
HairyOne
just joined
Posts: 12
Joined: Thu May 10, 2018 5:39 pm

Re: DST NAT return from same IP

Fri May 25, 2018 3:48 pm

Hi,

I need a little help. I have a Mikrotik CCR router with interface that has several public IP's. I also have DST-NAT rules (port forward) where i use public IP's and port forward (DST-NAT) external ports to internal server. I need to achieve that a return packet is sent with SRC IP = public IP that packet came in first place.
https://imgur.com/a/YAAyNTw

As you can see in the Wireshark example i have packet comming from 31.217.20.96 and it is sent to 194.152.192.156. Return packet is sent through default gateway and not with 194.152.192.156 as SRC IP.

Any advice?
Like other participants, I agree that your requirement is the actual way, how DST NAT and connection tracking are supposed to work.
SRC-NAT should be necessary only for outgoing connection, that are initiated by internal server.
Could it be that you have masquerade somewhere without defining in\out interfaces (ip addresses)? That might cause interesting effects.
 
sindy
Forum Guru
Forum Guru
Posts: 10206
Joined: Mon Dec 04, 2017 9:19 pm

Re: DST NAT return from same IP

Sun May 27, 2018 11:38 pm

Could it be that you have masquerade somewhere without defining in\out interfaces (ip addresses)? That might cause interesting effects.
It should not, because the chains of the NAT table are only consulted for the initial packet of each connection (connection-state=new). So even if a masquerade rule made the server see the request as coming from the router's LAN IP instead of the client's IP, the server would respond to the router's address and connection tracking would "un-src-nat" the destination, same like it "un-dst-nat's" the source if dst-nat was applied on the connection.

But it's like in the joke about the fallen concrete bridge where cement says "don't blame me, I wasn't there at all" - packet 873118 goes from port 11034 to port 16666, but packet 873120 goes to port 11034 but from port 63268. So the connection tracking cannot recognize that the two packets belong to the same connection (if they actually do which is questionable), and thus anything that relies on connection tracking, i.e. both NAT and connection marking, doesn't work.

So @ivugrinec, the first thing is to find out whether the packets actually do belong to the same connection at application level, and if they do, whether there is a way to make the server respond from the port on which it listens. If they do belong to the same connection but you cannot make the server respond from the same port, you'll need to record the address of the client to an address list logically attached to the IP address to which the request comes, and use a src-nat rule to set the right IP address to the first packet of the server->client flow which will be treated as a separate connection. As none of the connections will be "confirmed" (because the connection tracking will see only packets in one direction), shorter timeouts will apply.

And if several client connections may come from the same address, it is even more funny because the address-list-timeout will have to be long enough so that the client address would still be available in the list when the first response packet comes, but short enough that if another connection from the same remote address comes to a different local address, it would already be out of the previous list. If this is the case, enjoy the tuning process :-( .
 
Sob
Forum Guru
Forum Guru
Posts: 9121
Joined: Mon Apr 20, 2009 9:11 pm

Re: DST NAT return from same IP

Mon May 28, 2018 12:16 am

I need new glasses... or brain. I mean, I saw the ports, but if for some reason the reply packet wasn't handled by conntrack as such, and used another srcnat rule, the source port is not guaranted to stay unchanged, so it didn't look completely impossible. But internal server using different source port and thus creating new "connection" is the most simple explanation.

Who is online

Users browsing this forum: ChadRT, LdB, neko98 and 145 guests