Common Firewall Matchers and Actions
CLI reference:/ip/firewall/filter/ip/firewall/nat/ip/firewall/mangle/ip/firewall/raw/ipv6/firewall/filter/ipv6/firewall/nat/ipv6/firewall/mangle/ipv6/firewall/raw
The firewall tables filter, NAT, mangle and RAW have several things in common. Each rule counts the packets that it matches. The same commands show and reset these counters. The router checks the matchers of a rule in a fixed order.
The examples on this page use the IPv4 filter table. The counter commands are the same in the other tables and in the IPv6 firewall, for example /ip/firewall/nat/print stats or /ipv6/firewall/filter/print stats.
View the rule counters
Each firewall rule counts the packets and the bytes that it has matched. To see the counters, use print stats. This command shows the static rules of the forward chain:
/ip/firewall/filter/print stats static where chain=forward
Columns: CHAIN, ACTION, BYTES, PACKETS
# CHAIN ACTION BYTES PACKETS
;;; defconf: accept in ipsec policy
0 forward accept 0 0
;;; defconf: accept out ipsec policy
1 forward accept 0 0
;;; defconf: fasttrack
2 forward fasttrack-connection 2 457 15
;;; defconf: accept established,related, untracked
3 forward accept 2 457 15
;;; defconf: drop invalid
4 forward drop 0 0
;;; defconf: drop all from WAN not DSTNATed
5 forward drop 112 2
PACKETS is the number of packets that the rule has matched, and BYTES is their total size. The line above each rule is its comment.
To see the counters of particular rules, select them by their comment. This command shows the rules whose comment contains "drop invalid":
/ip/firewall/filter/print stats where comment~"drop invalid"
Use the counters to check that a new rule works: send traffic that the rule has to match, and look at its counters. If they do not grow, the rule does not match the traffic, or the packets do not reach the rule: an earlier rule of the chain has accepted or dropped them, or FastTrack handles the connection. In the default configuration, established connections are FastTracked and accepted at the start of the forward chain, so the rules after that do not see most of their packets.
Reset the counters
To set the counters of one rule or of several rules back to zero, use reset-counters with the rules that find selects. This command resets the rules whose comment contains "WAN":
/ip/firewall/filter/reset-counters [find comment~"WAN"]
To reset the counters of all rules of the table, use reset-counters-all:
/ip/firewall/filter/reset-counters-all
Both commands work on one table: /ip/firewall/filter/reset-counters-all does not change the counters of the NAT, mangle and RAW rules. Each table has its own commands, for example for the NAT table:
/ip/firewall/nat/reset-counters-all
Show or hide dynamic rules
A table can contain dynamic rules, marked with the D flag. These are rules that the router adds itself. An example is the rule with the comment "special dummy rule to show fasttrack counters": it processes no packets, and its counters show the traffic that FastTrack handles. You cannot change or remove that rule.
In the IPv4 tables, print lists the static and the dynamic rules, and print all does the same. To list only one kind, add static or dynamic:
/ip/firewall/filter/print dynamic
Flags: D - DYNAMIC
0 D ;;; special dummy rule to show fasttrack counters
chain=forward action=passthrough
/ip/firewall/filter/print static lists only the rules of the configuration. The rule numbers in the output belong to the list that the command printed, so they differ between print and print static. In the IPv6 firewall, only the NAT table has the static and dynamic switches. The IPv6 filter, mangle and RAW tables do not have these switches.
Order of the matchers
A rule matches a packet only when all its matchers match. The router checks the matchers of a rule in a fixed order, which does not depend on the order in which you write them.
The order matters for the matchers that keep a count or a state of their own, such as nth, limit and dst-limit: they see only the packets that have passed all matchers before them. For example, in a rule with protocol=icmp and limit, only ICMP packets use up the limit: the protocol matcher is not in the lists, and the router checks it before limit. In a rule with nth and limit, the limit counts only the packets that nth has selected. A packet over the limit does not match the rule, and the next rule of the chain processes it. log comes last: the rule writes a log entry only for a packet that all its matchers have matched.
For IPv4
- Source MAC address
- In and out interfaces
- In and out interface lists
- IP range
- Address type
- Address list
- TTL
- DSCP
- Length
- TLS
- IPv4 options
- Destination port
- Source port
- Any port
- TCP options
- TCP MSS
- ICMP codes
- Ingress priority
- Priority
- Packet mark
- Realm (routing table)
- HotSpot
- Connection mark
- Connection state
- Connection NAT state
- Connection bytes
- Connection limit
- Connection rate
- IPsec policy
- Helper
- String (content)
- PSD
- Layer7
- Random
- Nth
- PCC
- Limit
- Destination limit
- Log
For IPv6
- Address type
- Address list
- Source MAC address
- In and out interfaces
- In and out interface lists
- Hop limit
- DSCP
- Length
- TLS
- IPv6 header
- Destination port
- Source port
- Any port
- TCP options
- TCP MSS
- ICMPv6 codes
- Ingress priority
- Priority
- Packet mark
- Connection mark
- Connection state
- Connection NAT state
- Connection bytes
- Connection limit
- Connection rate
- IPsec policy
- Helper
- String (content)
- Random
- Nth
- PCC
- Limit
- Destination limit
- Log
The properties of the matchers are described in the CLI reference of each table, for example /ip/firewall/filter and /ipv6/firewall/filter.