Firewall Filter, NAT and Mangle · Topic 5 of 6

Mangle Chains and Actions

Learn RouterOS mangle: its five chains, custom chains, every mangle action including mark-connection and mark-packet, passthrough and common conditions.

  • 5 min
  • Includes a diagram
In a nutshell
  • Mangle has five chains: prerouting, input, forward, output and postrouting.
  • Mangle marks and modifies packets. It never allows or blocks traffic.
  • Mark types are connection, packet and routing.

What it is

Mangle marks packets and connections and changes some packet fields, so that other features can act on them.

Mangle never allows or blocks traffic. Its job is to label or modify packets. Queues, routing and filter rules then use those labels.

PacketarrivesMangle ruleconditions matchMark or changeconnection mark, packet mark, DSCPQueue or routeuses the mark

Mangle rules are in /ip firewall mangle. They follow the same rule layout as filter: chain, conditions, action.

How it works

Mangle has all five chains and a richer set of actions than filter.

Chains

ChainPosition in the path
preroutingBefore the routing decision, after tracking
inputFor packets addressed to the router
forwardFor packets passing through
outputFor packets the router creates
postroutingAfter routing, before the packet leaves

Mangle prerouting runs before dstnat. Mangle postrouting runs before srcnat. Custom chains work as in filter, using jump and return.

Marks

Three kinds of mark exist, and each has a different job.

MarkLives onTypical use
connection markThe tracked connectionClassify a whole connection once
packet markOne packetSelect a queue
routing markOne packetSelect a routing table

A mark is only a label stored in memory. It does nothing until another feature, such as a queue, a route rule or a filter condition, matches it.

Mangle actions

ActionEffect
mark-connectionSet new-connection-mark on the connection
mark-packetSet new-packet-mark on the packet
mark-routingSet new-routing-mark for policy routing
change-dscpSet the DSCP value to new-dscp
change-ttlChange the TTL as set by new-ttl
change-mssChange the TCP MSS to new-mss
set-prioritySet the priority field
strip-ipv4-optionsRemove IPv4 options from the packet
clear-dfClear the DF (do not fragment) flag
routeSend the packet to a gateway chosen with route-dst
fasttrack-connectionMark the connection for fasttrack
acceptStop processing the rules of this chain for this packet
passthroughCount the packet and move on
jump, returnMove into or out of a custom chain
logWrite a log entry and move on
add-src-to-address-list, add-dst-to-address-listStore the address in a list
sniff-tzsp, sniff-pcCopy the packet to a sniffer, either a TZSP stream or a PC

In version 6, mark-routing sets a routing mark that a route can use. In version 7, the routing mark is the name of a routing table, and routes in that table are used.

The passthrough option

Many mangle actions have a passthrough parameter. With passthrough=yes, the packet carries on to the following mangle rules. With passthrough=no, the rest of the rules in that chain are skipped for the packet.

Use passthrough=no when no later mangle rule needs the packet. This saves processing.

The efficient two step pattern

The first packet of a connection gets a connection mark from rule 1, then rule 2 turns it into a packet mark that a queue matches; later packets of the connection skip rule 1 and are marked by rule 2 alone. Marking every packet with a heavy rule is wasteful. A better design marks the connection once, then derives the packet mark from the connection mark.

/ip firewall mangle add chain=prerouting in-interface=ether2 protocol=tcp dst-port=80,443 connection-mark=no-mark action=mark-connection new-connection-mark=web-conn passthrough=yes
/ip firewall mangle add chain=prerouting connection-mark=web-conn action=mark-packet new-packet-mark=web-pkt passthrough=no

The first rule only sees packets of connections that have no mark yet. After the first packet, the second rule does the rest cheaply, because the connection mark is already set.

First packetno mark yetRule 1sets connection markRule 2sets packet markQueuematches packet mark

Marking traffic by direction

Replies arrive on a different interface, but they belong to the same connection. Because the connection mark is stored on the connection, a second rule can mark return packets as well, matching on the connection mark alone.

Common conditions

The common conditions are the same as in the other tables. They include src-address, dst-address, protocol, dst-port, in-interface, out-interface, connection-mark, packet-mark, connection-state and src-address-list.

For marks, the special value no-mark matches packets or connections that have no mark yet. This is the key to the two step pattern above.

Exam tip

Set connection marks in prerouting and output, then mark packets from the connection mark. It keeps the rule count and the CPU load low.

What to remember for the exam

Eight facts cover most questions on this topic.

  • Mangle has five chains: prerouting, input, forward, output and postrouting.
  • Mangle marks and modifies packets. It never allows or blocks traffic.
  • Mark types are connection, packet and routing.
  • passthrough=yes continues to the next rule. passthrough=no skips the rest of the chain.
  • Mark the connection first, then derive the packet mark from the connection mark.
  • connection-mark=no-mark matches a connection that has no mark yet.
  • A routing mark must be set before the routing decision.
  • change-dscp, change-ttl and change-mss rewrite header fields.
Watch out

A mark does nothing alone. If no queue, route or filter rule matches it, the mark has no effect.

Common mistakes

These four mistakes appear often.

Marking packets without a connection mark. Every packet runs the heavy rule, which wastes CPU.

Forgetting passthrough. With passthrough=no, later rules in that chain never see the packet, so a second mark is never made.

Marking in the wrong chain. A routing mark set after the routing decision cannot change the route.

Expecting mangle to block traffic. Only filter rules drop traffic.

Check yourself

Work through these three questions.

  • What does passthrough=no do in a mangle rule?

    It stops the packet from being checked against the later rules in the same mangle chain.

  • Why mark the connection before marking packets?

    The connection mark is set once, and later packets are marked cheaply from it.

  • Which mark is used to select a queue?

    The packet mark, which a queue matches.

Topic 5 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.