Community discussions

MikroTik App
 
thobias
newbie
Topic Author
Posts: 26
Joined: Thu Nov 30, 2017 8:45 pm

Add to address list and nat rule

Fri Oct 23, 2020 2:17 pm

Hi
As I understand packets first go through Filter and then NAT in the firewall but when I have a matching rule in Filter for adding src to address list it does not match.
Config below
/ip firewall filter
add action=add-src-to-address-list address-list=test address-list-timeout=none-dynamic chain=input comment=test dst-address=wanIP dst-port=88,2222 protocol=tcp
add action=accept chain=input comment="Accept established,related" connection-state=established,related
add action=drop chain=input comment="Drop all from WAN" in-interface-list=wanlist
add action=drop chain=forward comment="Drop invalid" connection-state=invalid
add action=drop chain=forward comment="Drop all from WAN not DSTNATed" connection-nat-state=!dstnat connection-state=new in-interface-list=wanlist
/ip firewall nat
add action=masquerade chain=srcnat comment="Default NAT" out-interface=wan
add action=dst-nat chain=dstnat comment="test" dst-address=wanip dst-port=88 in-interface-list=wanlist protocol=tcp to-addresses=192.168.1.55
When i try port 88 it does not match and put my IP in the address list but it does on port 2222 unless I disable or change the NAT rule.
Have tried it on several devices and latest long term release but it's the same behaviour.
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 11229
Joined: Mon Dec 04, 2017 9:19 pm

Re: Add to address list and nat rule

Fri Oct 23, 2020 2:32 pm

Does it work if you put there port 88 alone (rather than 88,2222)?
If you make a copy of that rule and just replace action=add-src-to-address-list by action=passthrough, does that added rule count packets with destination port 88? It is possible that the packets to port 88 actually don't arrive.
Last edited by sindy on Fri Oct 23, 2020 4:01 pm, edited 1 time in total.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Fri Oct 23, 2020 2:44 pm

What the heck is in-interface-list=WANlist??
You use dst-address=WANIP as well and in-interface=wan in another.

So lets be consistent.......... do you have one WANIP?
Is your wanip(s) dynamic or static/fixed?
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 11229
Joined: Mon Dec 04, 2017 9:19 pm

Re: Add to address list and nat rule

Fri Oct 23, 2020 4:03 pm

Sorry, I've missed the dst-nat part.

nat/dstnat happens before filter, so as you have redirected the traffic incoming to port 88 to a private address in dstnat, it became a transit one (from one router interface to another), and therefore it is handled by filter/forward, not by filter/input.
 
thobias
newbie
Topic Author
Posts: 26
Joined: Thu Nov 30, 2017 8:45 pm

Re: Add to address list and nat rule

Fri Oct 23, 2020 4:59 pm

Sorry, I've missed the dst-nat part.

nat/dstnat happens before filter, so as you have redirected the traffic incoming to port 88 to a private address in dstnat, it became a transit one (from one router interface to another), and therefore it is handled by filter/forward, not by filter/input.
Ok I thought it was the other way around with Filter first and then NAT. Not sure where I got that from then.
Is there any way to accomplish both dst-nat and saving to address list then?
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Fri Oct 23, 2020 7:02 pm

If you are trying to capture addresses attempting to connect to your server on those ports............

Well you could simply log them all? Would just provide viewing after the fact.

OR (add a rule before the accept rule to capture source?)
add action=add src to address list chain=forward comment="allow port forwarding" connection-nat-state=dstnat connection-state=new in-interface-list=WAN
add action=accept chain=forward comment="allow port forwarding" connection-nat-state=dstnat connection-state=new in-interface-list=WAN

hmm on second thought we need to pair that down a bit, ensure comment is correct and actually add an address list to send them too LOL.......
add action=add src to address list chain=forward comment="capture IP address" connection-nat-state=dstnat \
connection-state=new in-interface-list=WAN dst-port=******* address-list=portfwdsources
**** whatver the dst port, is on your dstnat rule ( not the translated to-port)!!
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 11229
Joined: Mon Dec 04, 2017 9:19 pm

Re: Add to address list and nat rule

Fri Oct 23, 2020 8:03 pm

Is there any way to accomplish both dst-nat and saving to address list then?
Of course. Two ways:
  • place the action=add-src-to-address-list rule to filter/input for port 2222 and to filter/forward for port 88 (with additional conditions respecting the direction and destination)
  • place the action=add-src-to-address-list rule to mangle/prerouting as mangle/prerouting handles the packet before dstnat (also here you may need to add some conditions).
This set of diagrams shows it all.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Fri Oct 23, 2020 9:59 pm

Interesting!! Lets debate the issue.
WHAT I THINK YOU ARE SAYING
So sindy if the person is looking to capture the addresses (source) for a dst nat there are two ways for a scenario of translated port?

1. add address list in the INPUT CHAIN with the dst-port used in the associated dst-nat rule?
2. add address list in the FORWARD CHAIN with the to-ports in the associated dst-nat rule?

Meaning that that the forward chain will never see the dst port?? (as port has been modifed in NAT before hitting the fw rules in the forward chain)
Meaning that the input chain will see the original dst port?? (as traffic passed firewall rule on the input side before hitting dst nat)

WHY I THINK YOUR ARE WRONG (2 reasons)
(1) The problem is looking at the diagrams, the dst-nat is done in PREROUTING which is before the input chain.

(2) I believe the capture is only possible in the forward chain...........as the traffic is never meant for the router and thus should be considered FORWARDED traffic (why even look for it in the input chain??).

This being the case, the only way to capture the source is the following
add action=add src to address list chain=forward comment="capture IP address" connection-nat-state=dstnat \
connection-state=new in-interface-list=WAN to-ports=******* address-list=portfwdsources
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 11229
Joined: Mon Dec 04, 2017 9:19 pm

Re: Add to address list and nat rule

Fri Oct 23, 2020 10:26 pm

Interesting!! Lets debate the issue.
The dst-nat rule matches only on dst-port=88. Hence packets towards the WAN IP:88 get dst-nated, and packets towards the WAN IP:2222 don't. So those to :88 have got a new destination address after the dst-nat operation. And the decision whether the packet is for one of router's own IP addresses, and thus should be handled by the [|NPUT|] chain, or whether it is for any other IP address than the router's own one, and thus should be handled by the [|FORWARD|] chain, is made after the [DST-NAT] stage of chain [|PREROUTING|].

To make it more complicated: whether a connection will be dst-nated and/or src-nated is decided when the first packet of a new connection is being handled by firewall table NAT in the corresponding (dstnat and/or srcnat) chain. But the [DST-NAT] block is changing the destination address of all packets in downstream (initiator=>responder) direction of the connections for which dst-nat handling has been imposed by the rule handling the first packet, and of all packets in upstream (responder=>initiator) direction of the connections for which src-nat handling has been imposed by the rule handling the first packet.

Also, the action=add-src-to-address-list rules should match on connection-state=new if used in mangle/prerouting, otherwise it would be adding the address to the list for every single packet, wasting CPU. Matching on connection-state=new is cheaper than matching on src-address=!test which would otherwise serve the same purpose. If used in filter, and placed after the "accept established" rule, this is not necessary.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Fri Oct 23, 2020 11:57 pm

Unfortunately, the english in your translation is more confusing than helpful.
So lets establish ground truths otherwise we are on quicksand!!

(1) DST-NAT is done in pre-routing.
(2) The first firewall filter is seen in the input chain, after prerouting.
(3) The packets are not headed to the router per se, they are meant to be forwarded to a server on the LAN.

The above and mine and thus probably wrong hypothesis supported by the following diagrams:
You do not have the required permissions to view the files attached to this post.
Last edited by anav on Sat Oct 24, 2020 12:05 am, edited 1 time in total.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Sat Oct 24, 2020 12:10 am

However we have to tackle the Bridge Packet flow diagram.
Or do we?? The answer I think is no, our entry point it letter I, as the traffic came in the WAN PORT not a bridge port.

Thea actual traffic arrives at the Router A.
The first question is this coming in on a bridge port interface - answer = NO (coming in on PHYSICAL wan port and thus bypasses traffic that is coming in from bridge ports)
The second question asked in the flow, is this MPLS (sounds like a disease, no clue) answer = NO
Thus the traffic heads towards........ IPV4 an IPV6 Traffic answer = YES
Then the traffic ENTERS at letter I, as the diagram above.

So based on that I am still asserting the only way to catch the information is on the forward chain with the TO-PORTS entry

In a nutshell, in pre-routing the traffic port translation is done. The traffic then goes to the forward filter rule .............do I allow traffic to proceed. YES>
 
Sob
Forum Guru
Forum Guru
Posts: 9188
Joined: Mon Apr 20, 2009 9:11 pm

Re: Add to address list and nat rule

Sat Oct 24, 2020 12:14 am

Bridging diagram applies only when there's bridge involved and it actually does some bridging. So for example bridge as incoming interface (when you have few ports bridged together as LAN) doesn't count.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Sat Oct 24, 2020 3:53 pm

Thats fine because I ignored the bridging flow diagram as it didnt apply. The fact of the matter is the incoming wan data goes to preouting gets dsntated (port translation) and connection tracking, heads tot he forward chain and there gets forward filtered............
 
User avatar
sindy
Forum Guru
Forum Guru
Posts: 11229
Joined: Mon Dec 04, 2017 9:19 pm

Re: Add to address list and nat rule

Sat Oct 24, 2020 4:15 pm

But the thing here is that the OP does not do port translation but port based redirection to another address. That's why the incoming connection to dst port 88, which gets dst-nated, is then handled in forward whereas the incoming connection to dst port 2222, which does not get dst-nated, is handled in input.
 
User avatar
anav
Forum Guru
Forum Guru
Posts: 22090
Joined: Sun Feb 18, 2018 11:28 pm
Location: Nova Scotia, Canada
Contact:

Re: Add to address list and nat rule

Sat Oct 24, 2020 5:28 pm

Ouch, how did I miss that............ I will have to go back and read again............sigh
At least I got to show off my ms Paint skills...........

Who is online

Users browsing this forum: mkx, sinisa and 37 guests