Mangle rules: Use for different WANs.

Hello Everyone!
I've got a service behind a Mikrotik on network 192.168.0.0/24 and two diferent "WANs". I want that every client who access through ether1 receives his anwser on same etherport.

I know that is possible with Mangle rules, but I have trougbles to which CHAIN and ACTION parámeters are correct for my partiular case.

I give you my configuration on below:

[admin@MikroTik] > export

jan/13/1970 00:29:02 by RouterOS 6.43.7

software id = YCY3-EMB7

model = RouterBOARD 1100x4

serial number = 793108AB61D1

/interface wireless security-profiles
set [ find default=yes ] supplicant-identity=MikroTik
/ip pool
add name=dhcp_pool0 ranges=192.168.0.100-192.168.0.254
/ip dhcp-server
add address-pool=dhcp_pool0 disabled=no interface=ether11 name=dhcp1
/ip address
add address=172.16.254.2/30 interface=ether1 network=172.16.254.0
add address=172.16.254.6/30 interface=ether2 network=172.16.254.4
add address=192.168.0.1/24 interface=ether11 network=192.168.0.0
/ip dhcp-server network
add address=192.168.0.0/24 dns-server=8.8.8.8,8.8.4.4,1.1.1.1 gateway=192.168.0.1
/ip firewall mangle
add action=mark-connection chain=forward connection-state=new dst-address=192.168.0.80 dst-port=80 in-interface=ether2 new-connection-mark=to-R2 passthrough=no
protocol=tcp
add action=mark-routing chain=output connection-mark=to-R2 new-routing-mark=to-R2 passthrough=no src-address=192.168.0.80
/ip route
add distance=1 gateway=172.16.254.5 routing-mark=to-R2
add distance=1 gateway=172.16.254.1

Check the PCC example, it’s all there: https://wiki.mikrotik.com/wiki/Manual:PCC

It’s primarily about load balancing, but in fact the actual load balancing is just two rules with per-connection-classifier. Most of the rest is common dual-WAN config you’re looking for.

I am a bit confused as I dont see any user going OUT on a particular etherport somehow getting returns on a different etherport???
and thus no special rules would be required.

However if the op is saying he has a server that people from outside the internet will be accessing and he/she wants to ensure that server returns go out the etherport they came in on then
the pcc load balancing you mention would keep it consistent?? Again wouldnt the router keep track of which etherport the traffic came in on?? (and thus no special rules required).

or are you saying pcc is simply a method to load balance the two ISP inputs and the side benefit is that both ISPs will be used, dont have to worry about routing preference (ie the pcc basically simplifies the outgoing routing choices)??

The wiki page @Sob refers to actually describes how to use connection-mark in general, whereas the per-connection-classifier, which as such can work even if it assigns routing-mark values directly, i.e. without connection marking, is shown on that page as just one of the possible criteria to assign the connection-mark. The other criteria, also mentioned there, is the WAN interface through which the initial packet of a connection came in.

So different criteria are used for connections initiated from the LAN side than for connections initiated from the WAN one: those initiated from LAN side get their connection-mark based on PCC, so they are distributed among the WANs; those initiated from WAN side get their connection-mark based on their source WAN so that the response LAN->WAN packets used the same WAN, and this is the part the OP asked about.

The question was not clearly answered (at least for me with limited understanding).
Wouldnt the router know which etherport the traffic came in on and thus send it back out that way?
It seems not from your response (and its good to learn that nothing (packet flow) is necessarily automatic in MT), and that the traffic would go out via the route rules which may not guarantee the same etherport out…

So the load balance method, although designed to load balance seems to have a side benefit of ensuring incoming on wanx, also goes out wan x.

No, it’s not automatic, router uses regular routes to find out where to send packets. With usual multi-WAN setup (different ISPs) you obviously want to send responses back the same way from where requests came, because it wouldn’t work otherwise. But it’s also possible to have asymmetric routing where each direction uses different path (don’t ask me for any good examples, I don’t remember needing something like that myself). The point is that one or the other isn’t always right or wrong.

Load balancing doesn’t have anything to do with it, except that I remember PCC example to also contain rules for this. :slight_smile:

Thanks a lot for your answer, it helps me a lot.

It is not exactly like you post, but it helps me a lot for find my solution.

You can see my solution below:

/interface wireless security-profiles
set [ find default=yes ] supplicant-identity=MikroTik
/ip pool
add name=dhcp_pool0 ranges=192.168.0.100-192.168.0.254
/ip dhcp-server
add address-pool=dhcp_pool0 disabled=no interface=ether11 name=dhcp1
/ip address
add address=172.16.254.2/30 interface=ether1 network=172.16.254.0
add address=172.16.254.6/30 interface=ether2 network=172.16.254.4
add address=192.168.0.1/24 interface=ether11 network=192.168.0.0
/ip dhcp-server network
add address=192.168.0.0/24 dns-server=8.8.8.8,8.8.4.4,1.1.1.1 gateway=192.168.0.1
/ip firewall mangle
add action=mark-connection chain=prerouting connection-mark=no-mark in-interface=ether2 new-connection-mark=R2_conn passthrough=yes
add action=mark-routing chain=prerouting connection-mark=R2_conn in-interface=ether11 new-routing-mark=to_R2 passthrough=yes

/ip route
add check-gateway=ping distance=1 gateway=172.16.254.5 routing-mark=to_R2
add distance=1 gateway=172.16.254.