How a Firewall Rule Works
Interface, direction, protocol, source, destination, port and action: how the firewall reads its rules, with a practical example on OPNsense.
A firewall makes decisions all the time: it lets a web page through, it stops an attempt to reach the internal network. It does not decide at random: it compares every packet with the rules written by the administrator. Understanding what a rule is made of, and how the firewall reads the rules, is the first step towards configuring a secure network. In this article we look at the fields of a rule, the three possible actions, the role of the state table and the evaluation order, with a complete example on OPNsense: a guest network that can browse the Internet but cannot reach the LAN.
What a firewall rule is
A firewall rule is a sentence in two parts: a condition, which describes a certain type of traffic, and an action, which says what to do when a packet meets the condition. "TCP traffic coming from the guest network and going to port 443 of any destination: let it through" is a rule. The packet must meet all the criteria of the condition: a single different field, such as the port, is enough for the rule not to apply.
In the examples we use OPNsense, an open-source firewall based on FreeBSD and on the pf packet filter. Rules are managed under Firewall › Rules, with one tab for each interface (WAN, LAN and the other networks). Field names are those of the web interface; the concepts apply, with small differences, to other firewalls as well.
The fields of a rule
The rule editing page has many options; these are the essential ones (among the others is the IP version, IPv4 or IPv6):
- Action: Pass, Block or Reject, as we will see shortly.
- Interface: the network the rule applies to, for example LAN or GUEST.
- Direction: in or out. Interface rules normally apply to incoming traffic, that is, to packets entering the firewall from that network. This is the default.
- Protocol: TCP, UDP, TCP/UDP, ICMP or any.
- Source: where the traffic comes from. It can be an address, a network, an alias or a predefined entry such as "GUEST net", meaning the whole subnet of the interface.
- Destination: where the traffic is going, with the same options as the source, plus entries such as "This Firewall", which covers all the addresses of the firewall itself.
- Destination port range: the requested service, for example 443 for HTTPS or 53 for DNS. There is also a source port, but it should almost always be left as any: clients pick random source ports.
- Quick: when enabled, the rule applies as soon as the packet matches, without reading the following rules. It is enabled by default.
- Log: writes the packets that match the rule to the log.
Pass, Block or Reject
There are three actions. Pass lets the packet through. Block drops it silently: the sender gets no reply and, for a TCP connection, keeps retrying until it times out. Reject drops it but informs the sender, with a TCP RST segment for TCP connections or with an ICMP destination unreachable message for UDP.
On the WAN, Block is preferred, so as not to give information to anyone probing the network from outside. On internal networks Reject is often more practical: an application attempting a forbidden connection gets an error at once instead of waiting.
Stateful firewall: replies need no rule
OPNsense, like almost every modern firewall, is stateful: it keeps track of connections. When a Pass rule lets the first packet of a connection out, the firewall records an entry in the state table: protocol, addresses and ports of both ends. The following packets of the same connection, including the server's replies, are recognised through this entry and pass without being compared with the rules again.
There are two practical consequences. First: to let the guest network browse, no rule is needed on the WAN interface for the replies of websites, which come back through the state. Second: a rule always describes who opens the connection. If LAN devices may contact devices on the guest network, it does not mean that guests may contact the LAN: these are two different flows, each with its own rule on the interface where it starts.
State also has a less intuitive effect: a new block rule does not interrupt connections that are already established, which carry on as long as their entry stays in the table. The entries can be viewed and reset under Firewall › Diagnostics › States.
Order matters: top to bottom, first match wins
On each interface the rules form an ordered list. When a packet comes in, OPNsense compares it with the rules from top to bottom and, with the Quick option enabled, applies the first rule that matches: the following rules are not even read.
The pf filter on which OPNsense is based actually works the other way round: without quick, the last matching rule wins. That is why the option is enabled by default on interface rules, and it is best left that way: the list reads as a sequence of decisions, from the most specific to the most general.
OPNsense also has Floating rules, valid for several interfaces, and rules for interface groups: they are evaluated before those of the single interface, while some automatic system rules come before everything else. For a simple configuration, the interface tabs are enough.
The rule you cannot see: the default block
What happens to a packet that matches no rule? In OPNsense it is blocked. At the end of every list there is an implicit block rule, the so-called default deny: whatever is not explicitly allowed is denied.
That is why a newly created interface lets nothing through until rules are added. The LAN is an exception only because the installation creates two rules that allow the LAN network to reach any destination, one for IPv4 and one for IPv6, plus an anti-lockout rule that guarantees access to the web management interface. On the WAN interface, on the other hand, there is no Pass rule at all: nothing comes in from outside, except replies to connections opened from inside.
Practical example: the guest network
Illustrative example; private network addresses as defined in RFC 1918. An office has a LAN for its computers (192.168.10.0/24) and a Wi-Fi network for guests (192.168.60.0/24), connected to a firewall port assigned as interface OPT1 and renamed GUEST. Guests must be able to browse over HTTPS and use the firewall's DNS, but must not reach the LAN or the firewall's management interface.
First, an alias is created under Firewall › Aliases: it is called RFC1918, its type is Network(s) and it contains the three private networks 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Then, under Firewall › Rules › GUEST, four rules are written in this order:
| # | Action | Protocol | Source | Destination | Dest. port | Description |
|---|---|---|---|---|---|---|
| 1 | Pass | TCP/UDP | GUEST net | GUEST address | 53 | Firewall DNS |
| 2 | Block | any | GUEST net | This Firewall | any | No access to the firewall |
| 3 | Block | any | GUEST net | RFC1918 (alias) | any | No access to internal networks |
| 4 | Pass | TCP | GUEST net | any | 443 | HTTPS browsing |
| — | Block | implicit rule: everything else | Default block | |||
The order is not random. Rule 1 is at the top because the DNS request goes to an address of the firewall, which rule 2 would block: placed after it, DNS would not work. Rule 2 protects every address of the firewall, including the public WAN address, which is not covered by the RFC1918 alias and which rule 4 would otherwise make reachable on port 443. Rule 3 closes access to the LAN and to any other private network, including future ones. Only after these prohibitions does rule 4 open browsing towards everything else, that is, the Internet.
Let us try three packets. A guest smartphone (192.168.60.25) tries to open a shared folder on a LAN PC, 192.168.10.20 on TCP port 445: rules 1 and 2 do not match, rule 3 does, and the packet is blocked. The same smartphone opens a website at 203.0.113.10 (an address reserved for documentation, RFC 5737) on port 443: the first three rules do not match, rule 4 does, the packet passes and an entry is created in the state table, which lets the reply back in. Finally it tries an SSH connection, port 22, to the same server: none of the four rules matches and the default block applies.
The firewall's DHCP service, when it is enabled on the interface, adds the automatic rules it needs by itself. Other services are opened only if needed: for example HTTP on TCP port 80, or UDP 443, used by HTTP/3; without that rule, browsers fall back to HTTPS over TCP. If the guest network also uses IPv6, the internal networks alias must include the organisation's IPv6 prefixes.
Logs, aliases and descriptions
Three tools keep rules manageable over time:
- Logs. With the Log option enabled, packets matching the rule appear under Firewall › Log Files › Live View, with interface, addresses, ports and the rule applied. It is the quickest way to understand why a service does not work; it is worth logging block rules above all.
- Aliases. An alias gives a name to a group of addresses, networks or ports. A rule that uses "RFC1918" or "PRINT_SERVERS" is easy to read, and when an address changes you edit the alias, not dozens of rules.
- Descriptions. The Description field should say why the rule exists: in six months it will help whoever has to change it.
The most common mistakes
- Rule on the wrong interface. Guest traffic to the Internet is filtered on the GUEST interface, where it comes in, not on the WAN, where it goes out.
- Generic rule too high up. A Pass rule to any placed before the prohibitions makes them useless, because the first match wins.
- Source port set. Entering 443 as the source port instead of the destination port produces a rule that almost never matches.
- Rules for replies. Adding rules on the WAN to let replies back in is pointless, thanks to the state table, and often dangerous.
- Forgotten states. After adding a block, a connection that is already open may keep working: check it and, if necessary, reset the states.
- Changes not applied or not verified. After saving, changes must be applied, then checked with real tests and with the log.
Conclusions
A firewall rule describes a precise kind of traffic, with interface, direction, protocol, source, destination and port, and assigns it an action. Three mechanisms do the rest: the state table, which lets replies back in; the top-to-bottom order, in which the first matching rule wins; and the default block, which stops whatever has not been allowed. With a few well-ordered rules, readable aliases and a log checked regularly, the network stays secure and easy to understand.
The video
Further reading
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Video course: Capire i Firewall con OPNsense (video course in Italian)
- Video course: VLAN con OPNsense®: progettazione e configurazione (video course in Italian)
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Specialization: Firewalls & Network Security
Technical sources
- Documentazione ufficiale di OPNsense — Firewall › Rules — Rule fields, Pass, Block and Reject actions, the Quick option, evaluation order (Floating rules, interface groups, interfaces) and automatic rules.
- Documentazione ufficiale di OPNsense — Firewall › Aliases — Host, network and port aliases for use in rules.
- FreeBSD — pagina di manuale pf.conf(5) — The pf packet filter: the last matching rule wins unless a rule is quick; state keeping (keep state); block return.
- RFC 1918 — Private addresses for internal networks (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16).
- RFC 5737 — IPv4 address blocks reserved for documentation (203.0.113.0/24, used in the examples).
- IANA — Service Name and Transport Protocol Port Number Registry — Ports of the services mentioned: domain 53, http 80, https 443, ssh 22, microsoft-ds 445.
Standards and official documentation available from the respective organisations.