Firewall Filter, NAT and Mangle · Topic 6 of 6

Mangle Advanced and Extra Conditions

Learn the advanced and extra RouterOS conditions: address lists, connection bytes, PCC, nth, time, limits, address type and layer7 matching.

  • 5 min
  • Includes a diagram
In a nutshell
  • Advanced and Extra conditions are ordinary parameters in the console.
  • A rule matches only when all its conditions match.
  • PCC hashes connection fields into groups, such as both-addresses:2/0.

What it is

Beyond addresses and ports, firewall rules have advanced conditions that match behaviour, such as size, rate, time or a hash of the connection.

In WinBox these appear on the Advanced and Extra tabs of a rule. In the console they are ordinary parameters. They work in filter, NAT and mangle, but they are most common in mangle, where they classify and split traffic.

Packetreaches ruleBasic conditionsaddress, port, interfaceAdvanced conditionssize, rate, hash, timeActionapplied if all match

A rule matches only when every condition matches, so each extra condition makes a rule more specific.

How it works

Each advanced condition answers a different kind of question.

Conditions about lists, size and content

ConditionMatches
src-address-list, dst-address-listAn address in a named list
connection-bytesA connection that has transferred a given range of bytes
connection-rateA connection whose speed is in a given range
packet-sizeA packet whose size is in a given range
contentA packet whose data contains a given string
layer7-protocolA packet whose data matches a regular expression
src-mac-addressThe source MAC address
dscp, ttl, tcp-flagsA header field value

Address lists are filled by the add-src-to-address-list and add-dst-to-address-list actions, or by hand. A single rule can then match hundreds of addresses cheaply.

connection-bytes is useful to lower the priority of long downloads. A rule matches only after a connection has moved a set number of bytes, so small web requests are untouched.

Conditions for sharing and limits

ConditionMatches
per-connection-classifierA share of connections, chosen by a hash
nthEvery nth packet
randomA random percentage of packets
limitPackets up to a rate, with a burst
dst-limitPackets up to a rate for each destination
connection-limitHosts with more than a number of connections
timeA time of day and days of the week
src-address-type, dst-address-typeThe kind of address, such as local, broadcast or multicast

Per connection classifier

LAN hosts reach a router that uses two PCC rules, both-addresses:2/0 and 2/1, to mark connections wan1-conn and wan2-conn, each then routed to its own uplink. PCC splits connections into groups. It hashes chosen fields and compares the result with a remainder, so each connection always lands in the same group. This is the standard way to load balance over several uplinks.

/ip firewall mangle add chain=prerouting in-interface=ether2 connection-mark=no-mark per-connection-classifier=both-addresses:2/0 action=mark-connection new-connection-mark=wan1-conn passthrough=yes
/ip firewall mangle add chain=prerouting in-interface=ether2 connection-mark=no-mark per-connection-classifier=both-addresses:2/1 action=mark-connection new-connection-mark=wan2-conn passthrough=yes

The value both-addresses:2/0 means two groups, and this rule takes group zero. The second rule takes group one. A later rule can then turn each connection mark into a routing mark, so each group uses its own uplink.

New connectionfrom the LANPCC hashgroups 0 and 1Connection markwan1 or wan2Routing markchooses the uplink

Nth, random and limit

A single LAN connection passes a router with an nth=2,1 rule, which sends every second packet to uplink 1 and the other packets to uplink 2, splitting the session across both uplinks. nth=2,1 matches every second packet. It counts packets, not connections, so on its own it splits the packets of one connection across paths. PCC is the usual choice for balancing connections.

limit matches packets only up to a given rate. Combined with a drop action, it gives simple rate limiting, and combined with a log action it stops a flood of log lines.

Time

The time condition matches a window of the day and chosen days. It uses the router clock, so the system time and time zone must be correct.

Layer 7

layer7-protocol matches packet data against a regular expression stored in the layer 7 protocols menu. It looks inside the early packets of a connection. It is expensive for the CPU and easy to evade, so use it sparingly and only after cheaper conditions have narrowed the traffic.

Watch out

Put cheap conditions such as address, protocol and port first, and use layer7-protocol only to confirm. Otherwise every packet pays the full cost.

Address type

dst-address-type=local matches addresses that belong to the router. broadcast and multicast match those types. These conditions save you from listing every router address by hand.

What to remember for the exam

Eight facts cover most questions on this topic.

  • Advanced and Extra conditions are ordinary parameters in the console.
  • A rule matches only when all its conditions match.
  • PCC hashes connection fields into groups, such as both-addresses:2/0.
  • PCC keeps each connection on the same uplink, so it is used for load balancing.
  • nth matches every nth packet and does not follow connections.
  • limit matches up to a packet rate, and dst-limit does so per destination.
  • layer7-protocol is costly and should follow cheaper conditions.
  • time depends on a correct router clock.
Exam tip

For load balancing over two uplinks, use PCC with connection marks first, then routing marks, instead of nth.

Common mistakes

These four mistakes appear often.

Using nth to balance connections. It counts packets, so without a connection mark the packets of one connection are split across uplinks, which breaks the session.

Putting layer7 first. The expensive match then runs for all traffic.

Forgetting connection-mark=no-mark with PCC. Packets of marked connections are classified again.

Trusting the time condition without a correct clock. The window then opens at the wrong hour.

Check yourself

Work through these three questions.

  • Why is PCC suitable for load balancing over two uplinks?

    It hashes connection fields into groups, so every packet of one connection lands in the same group and uses the same uplink.

  • Why should layer7-protocol come after other conditions?

    It is costly, so cheaper conditions should narrow the traffic first.

  • What does connection-bytes allow you to do?

    It matches a connection only after it has transferred a set range of bytes, so you can treat long downloads differently.

Topic 6 of 6 in Firewall Filter, NAT and Mangle

Test yourself on Firewall Filter, NAT and Mangle

The full MikroTik MTCTCE question bank has 479 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by MikroTik. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.