In short: the RouterOS firewall filter sorts every packet into one of three chains: input (to the router), forward (through the router), output (from the router). In each chain the rules are read top to bottom and the first one that matches with an action like accept or drop ends the game; if none matches, the packet passes. That’s why a new rule always goes above the final drop, never below.

What are the input, forward, and output chains?

They are three lists of rules, one for each direction a packet can take relative to the router. They exist by default and can’t be deleted.

  • input: packets that enter the router and have one of its addresses as destination. WinBox, SSH, the ping to the router, DNS requests to the router, WireGuard traffic to the router;
  • forward: packets that cross the router. The LAN PC browsing, the port forward to the internal server;
  • output: packets that are born in the router and leave through an interface. The ping launched from the router’s terminal, its NTP and DNS requests, the replies the router sends to WinBox.

The point many miss: the packets that cross the router do not pass through the input chain. If you want to stop a LAN PC from reaching a site, the rule goes in forward. In input no packet from that PC heading to the internet will ever see it.

The rules are in /ip firewall filter for IPv4 and in /ipv6 firewall filter for IPv6: two separate firewalls, with the same three chains. You can also create chains of your own (just write a new name in chain=) and jump into them with action=jump; action=return brings it back.

In what order are the rules read?

Top to bottom, in the order you see them with print. The router compares the packet with the first rule of the chain; if it doesn’t match it moves to the second, and so on. At the first rule that matches it runs the action and, if the action is terminal, it stops reading that chain.

It doesn’t matter which rule you wrote first, it matters where it sits. A perfect rule in the wrong place is useless.

Which actions end the game and which don’t?

The manual divides them like this:

  • end the chain: accept (the packet passes, no later rule sees it), drop (dropped silently), reject (dropped with an ICMP reply, or a TCP reset with reject-with=tcp-reset), tarpit (keeps the connection hanging);
  • continue to the next rule: log (writes a line in the log), passthrough (just counts), add-src-to-address-list and add-dst-to-address-list (note the address down in a list);
  • move the packet: jump sends it to another chain, return brings it back to the one it jumped from.

That’s why port knocking works: the rule that puts you in the address list opens nothing, it notes you down and lets the packet drop down to the drop. From outside the port looks closed like the others.

What happens if no rule matches?

The packet is accepted. The default policy of RouterOS’s chains is accept, and it can’t be changed: if you want to close, you write the closing yourself, as the last rule.

It follows that a router with no rules is an open router. After a /system reset-configuration no-defaults=yes (teaching reset, see the Basic hardening howto) the filter is empty and WinBox answers anyone who reaches it.

What does the factory firewall look like?

It’s an “accept what you need and drop the rest” firewall toward the router, and “let out, don’t let in” toward the LAN. On a routerboard with the factory configuration you find rules commented defconf, in this order (chain and comment):

input    defconf: accept established,related,untracked
input    defconf: drop invalid
input    defconf: accept ICMP
input    defconf: accept to local loopback (for CAPsMAN)
input    defconf: drop all not coming from LAN
forward  defconf: accept in ipsec policy
forward  defconf: accept out ipsec policy
forward  defconf: fasttrack
forward  defconf: accept established,related, untracked
forward  defconf: drop invalid
forward  defconf: drop all from WAN not DSTNATed

The scheme is the same in both chains: first the replies to open connections, then the safe drops and the exceptions, finally the drop. The CHR starts with no factory configuration, so with no firewall.

The practical consequence is one only: every rule you add for traffic from the internet must sit above drop all not coming from LAN. Below it, that packet has already been dropped.

How do I put a rule in the right spot?

With place-before while you create it, or with move afterward. add puts the new rule at the bottom of the list, that is below the drop: it’s the most common mistake.

/ip firewall filter
add chain=input protocol=tcp dst-port=22 src-address=10.6.0.50 action=accept \
    comment="SSH from the lab PC" \
    place-before=[find comment="defconf: drop all not coming from LAN"]

⚠️ Warning: 10.6.0.50 is my lab’s PC, on the WAN 10.6.0.0/12 with gateway 10.0.0.1. Use the address of your own workstation; if you’re unsure about the network, check it with the IP calculator.

To move a rule that’s already there, move takes the rule to move and the one to put it in front of:

/ip firewall filter
move [find comment="SSH from the lab PC"] [find comment="defconf: drop all not coming from LAN"]

Refer to the rules with find and a comment, not with the number. The numbers are assigned by the last print of the session: in a pasted command, without that print, the number may not exist or may point to another rule. In WinBox and WebFig you change the order by dragging the row in the Filter list.

What are the rules with the D?

They are dynamic rules: RouterOS creates them on its own, you didn’t write them and you don’t save them with the configuration. You see them with the letter D in the flags, mixed in with yours in the print; with print where dynamic you see only those.

The most famous is the first row of the filter when there’s a fasttrack rule: special dummy rule to show fasttrack counters. It filters nothing. It shows how many packets are going through the fasttrack fast lane, which skips the rest of the firewall. You can’t move it: a move … 0 ends with cannot move builtin, as we saw in the DoH howto. Instead add … place-before=0 works: in the lab the new rule ended up on top and the dummy dropped to second place. And if you remove the fasttrack rule, the dummy stays there until the reboot: it does nothing, don’t worry. Others are added by services like Kid Control.

How do I figure out which rule is hitting a packet?

From the counters. Each rule counts the bytes and the packets that match it:

/ip firewall filter print stats
/ip firewall filter reset-counters-all

The method is this: you zero the counters, redo the test (the ping, the WinBox connection, the page that won’t open), reprint. The rule whose counter went up is the one that decided. If the final drop goes up and not your accept, your accept is below, or it doesn’t match what you think.

Why do you start from established and related?

Because most packets belong to connections already accepted, and it’s best to let them out of the chain at the first rule. The connection tracking remembers every connection and gives each packet a state: new opens a connection, established belongs to one already seen, related is tied to an existing connection (an ICMP error, the FTP data), invalid has no clear state and should be dropped, untracked skipped the tracking because of a RAW rule.

This way the rules that decide who gets in work only on the new packets. The connection register deserves a card of its own.

What are the typical mistakes?

The rule added at the bottom

You create the accept for the management port with a plain add, the rule ends up below the final drop, and its counter stays at zero forever. It’s not broken: nobody reaches it. The fix is a move or, better, the place-before from the start.

The too-broad accept on top

An accept in-interface-list=LAN in forward, put above everything, lets through even what you wanted to block further down: the hotspot guests toward the NAS, the PCs toward the encrypted DNS servers. The specific blocking rules go above the ones that accept generically.

Locking yourself out

A drop in input written before the accept for your workstation, and the WinBox session dies. Before touching the input chain remotely, turn on Safe Mode (the Safe Mode button in WinBox, Ctrl+X from the terminal): if the connection drops, the router undoes the changes on its own.

For all the rule parameters there’s the MikroTik manual: Filter.

Where do I use it?

Tested in the lab on a routerboard with RouterOS 7.24.5 (stable): the order of the defconf rules read from /system default-configuration print; log, add-src-to-address-list, and passthrough that continue (counters); an accept rule added below a drop that’s useless, then moved with move and find (the port opens); place-before; the fasttrack dummy rule with cannot move builtin and place-before=0; reset-counters-all.

Frequently asked questions

What’s the difference between the input chain and the forward chain?

The input chain sees the packets destined to the router itself, for example WinBox or SSH; the forward chain sees the packets that cross the router, for example a LAN PC browsing.

What does RouterOS do if no firewall rule matches?

It accepts the packet. The default policy is accept: to close a chain you need an explicit drop rule at the bottom.

Why doesn’t my accept rule work?

Almost always because it sits below a drop rule: the rules are read top to bottom and the first that matches decides. Check with /ip firewall filter print stats which counter goes up and move the rule with move or recreate it with place-before.

Does the log rule block the packet?

No. log, passthrough, and the actions that add to an address list record or note down and then pass the packet to the next rule. The ones that end the chain are accept, drop, reject, and tarpit.

What’s the dynamic rule on top with the fasttrack counters?

It’s a dummy rule that shows how much traffic goes through the fasttrack. It filters nothing and can’t be moved.