Two networks with different IP ranges in MikroTik

As CGGXANNX said, only create specific forward rules for the intended direction (the direction in which the connection is allowed to be initiated), and return traffic is always taken care of by the usual "accept established/related" rule.

No need for mark/mangle for this requirement.

This is precisely the point that is a bit unclear to me.

A client on LAN1 sends a request to a server on LAN2 via TCP port 3389, and this is accepted based on the initial firewall rules.

Subsequently, the server on LAN2 sends its response back to the client on LAN1.

This is exactly the kind of activity visible in software like Wireshark, where one observes LAN1 acting as the source at times and the destination at others, and vice versa.

So, in this case, the response is sent from LAN2 to LAN1, and logically, according to the rules, it should be dropped.

How, then, is the connection successfully established?

No.

The firewall works by first identifying the connection a packet belongs to based on the conntrack table then applying the rules.

For the initial contact LAN1->LAN2, there is no connection in the conntrack table with which the packet can be associated, so the connection-state is new. The usual rule for allowing established connections doesn't apply, and therefore the full ruleset is traversed.

Once this initial packet is allowed, the connection state is established.

Now, when the response packet comes along, it is associated with the conntrack entry already created when the initial packet was encountered, which has state established. The rule allowing established or related packets applies. The packet is accepted.

(This is, btw, why we have connection tracking and what we call a "stateful firewall")

Because an usual firewall filter implementation should have these rules, here is an example from the default MikroTik defconf firewall:

placed above the 4 custom rules that we added above. The response packets sent from LAN2 to LAN1 will be recognized by connection tracking (conntrack) as being part of a previously established connection. The packet will have connection-state=established (as opposed to connection-state=new for the very first packet of the connection). The packets having the established state will be caught by the rules pointed at by the blue arrows above (one rule for the input chain, and one for the forward chain) and will be accepted.

Once accepted by the rule, the other rules below that rule will be skipped. Which means the response packets will not be hit by the drop rules at all. With the accept established, related rule present in the firewall, your drop rules normally only have to handle the very first packet of each connection (the one with connection-state=new). This is also very efficient, because you can have hundreds of drop rules, but they normally only have to check 1 packet per connection. If you are downloading 14MB for example, then only 1/10000 of the packets needs to be checked by the drop rules, the rest only have to be checked by one single rule, the accept established, related rule that should be placed at the top of the chain.

Excellent.

But does this same thing happen with Mangle rules? I ran a test:

Mangle Rule 1 marks the connection from Lan1 to Lan2 as "Lan1ToLan2". Passthrough is false.

Mangle Rule 2 marks the connection from Lan2 to Lan1 as "Lan2ToLan1". Passthrough is false.

When these rules are enabled, as soon as a client sends a request via port 3389, the connection appears in the "Connections" list marked with the first rule's label. However, as soon as the server responds, the connection is marked with the "Lan2ToLan1" label.

So, based on your explanation and my test, MikroTik's behavior differs between Mangle rules and Filter rules.

Because you are missing recommended conditions on your mark-connection mangle rules. Usually, you should write your mark-connection rule with the extra properties connection-mark=no-mark connection-state=new.

This assures that the marking is only applied once per connection, when the first packet of the connection is seen only. At that moment, both conditions connection-mark=no-mark connection-state=new are true.

Once the connection mark has been set, subsequent packets of the same connection will have the properties connection-state=established, as well as connection-mark=xyz. Which means they will no longer be matched by the mark-connection mangle rules having connection-mark=no-mark connection-state=new anymore.

In short, you need to edit your mangle rules and extend them with the above conditions, and you'll see that the return packets are no longer affected by the 2nd mangle rule.

I learned what was necessary to manage this situation and realized the importance of paying closer attention to the details.

I hope this post proves useful to other users in the future.

Thanks to you and other friends here, for participating in this post.

Just another remark, if you don't mind: the use and usefulness of mangle rules is restricted (in various non-obvious ways) when fasttrack is used.

So e.g. in the set of rules shown by CGGXANNX, the rule fasttrack-ing connections should be disabled if mangle is used. (As a first approximation.)

Again, for your specific use case mangle is not needed, and I would venture that (in production) the use of mangle is discouraged if it's not necessary for some reason.

Yes, I completely agree with you.

I use them solely for packet marking, managing bandwidth for production servers and other clients, and for queueing purposes.

In this specific instance, since I didn't quite grasp why the two networks weren't communicating, I was testing various configurations.

however, thanks to the explanations provided by you and others, I removed all the unnecessary Mangle rules and met my requirements using Filter rules instead.

Thank you.