A guide for the configuration of OPNsense firewall rules and how to write your own rules, as well as port-forwarding

This is the 4th part in my homelab rebuild project, see Homelab Infrastructure Rebuild Overview for context. It is really only necessary to have an OPNsense instance deployed for this, which OPNsense Firewall Deployment, Configuration, and Segmentation with Proxmox explains how to do this. If you’ve followed the guides part-by-part, you may already have a couple firewall rules configured.

Firewall Ruling

Firewall rules in short are rules that decide what traffic is allowed to go where. It blocks/rejects and allows traffic based on constraints that it matches including port, protocol, and IP. Firewalls usually offer two options when stopping traffic, block or reject/deny. Generally, it is best to block versus reject. This is because blocking traffic will drop packets, and not provide any information to the client attempting to connect, which is useful in a security context. Reject will drop a packet but send an ICMP port unreachable (UDP traffic) or TCP RST (TCP traffic) error message to the connecting client letting them know their traffic was blocked.

Note that Firewall rules are checked against the table in a descending manner, until the traffic matches one. For example, If you want to allow all of a certain type of traffic, but deny one specific part, but the deny rule above the allow rule, otherwise the traffic will match the allow rule first and you will allow something you do not want. The order of your rules are very important!

Firewall rules universally share these properties: Interface, Action, Direction, Version, Protocol, Source, Source Port, Destination, and Destination Port. The table below explains each property.

PropertyDefinition
InterfaceThe network interface the rule applies to, and provides the overall context for the rule. Typically WAN, LAN, or a VLAN interface.
ActionWhat the firewall decides will happen to the traffic that matches the rule: Pass/Allow, Block, Reject/Deny. Firewalls may use different verbiage, so pass/allow are the same as well as reject/deny
DirectionA constraint for if the traffic is going in to the interface or out of the interface, usually determined by the verbiage of In or Out. Most rules you write are going to be In rules.

I’ll note that this part confused me for a while, so it helps to look at rules from the context of the interface and the firewall. A traffic flow to the internet, originating from a server as an example:

DMZ server -> DMZ interface -> Firewall -> WAN interface -> Internet
Traffic is going IN to the DMZ interface and OUT of the WAN interface.
VersionThe IP version the rule applies to, IPv4, IPv6, or Both
ProtocolThe type of traffic the rule applies to, TCP, UDP, TCP/UDP, ICMP, Any, etc
SourceWhere the traffic originates from, can be a single IP, a subnet, or a grouping of IPs
Source PortThe port that the traffic originates from. In most cases you will use Any as connecting clients pick a random port to initiate a connection.
DestinationWhere the traffic is going, can be a single IP, a subnet, or a grouping of IPs
Destination PortThe port of the service being reached, like 443 for HTTPS or 22 for SSH

Stateful vs. Stateless Firewalls

Stateful FirewallStateless Firewall
Keeps a state table and only inspects the first packet against a firewall rule. The state table maintains a record of a connection between two devices and allows subsequent traffic between the two devices automaticallyInspects traffic on a packet-by-packet basis

Most firewalls are stateful, including OPNsense. This is because they are more secure, as stateless firewalls can be bypassed with traffic spoofing from other devices as there is no state table to record connection. Stateless firewalls have to leave return ports open, so packets that are made to look like replies can get through. Additionally, stateful firewalls are easier to configure as they typically only need one rule for a connection, while stateless may require multiple for inbound and outbound traffic flows as two devices communicate.

Stateful firewalls do however require more computing power due to a maintained state table. Therefore, stateless firewalls are used in high-traffic flows as front layer to a stateful firewall to get rid of obviously unwanted traffic as to not waste computing power. The rest of the traffic is handed to the stateful firewall.

Best Practice

  • Use stateful firewalls, and this may be all you need. For a high traffic flow, configuring a stateless firewall in front of a stateful firewall can be used to save computing power on the stateful firewall
  • Write inbound rules as often as possible; Write rules only for the side that starts the connection
  • Block everything by default, and write allow rules only for what is needed. (OPNsense blocks by default)
  • Create a diagram of your allowed traffic flows to help visualize what is allowed through, and look for holes
  • Block any connection to the firewall’s management interface that is unnecessary. Ideally have one trusted area or device where the management interface is accessible

Creating a Firewall for SSH (EXAMPLE)

I want to allow machines on my TRUSTED VLAN to be able to SSH into a machine on my INTERNAL VLAN. Here’s how I would configure it:

  1. Navigate to your OPNsense management GUI
  2. Navigate to Firewall | Rules
  3. Click +
  4. Configure according to the table:
SettingOptionWhy
EnabledYesYou must enable the firewall rule in order for it to be active
CategoriesNothing SelectedOPNsense grouping option, you may use it if you like
DescriptionTRUSTED SSH to INTERNALDescriptions are optional but highly recommended so you can quickly see the purpose of a rule you have
Interface (rule)TRUSTEDSelect the interface for which the traffic would be inbound. In this case, the traffic would be inbound to the TRUSTED VLAN interface
Interface (origin)anyOrigin is largely only relevant for outbound rules, keep it set to any
QuickYesTry to keep all rules as quick in order to have consistency in your firewall rules being checked in a descending order. Unchecking this makes it such that the last matching rule is the chosen rule.
ActionPassPass to allow the traffic.
DirectionInRule is on the TRUSTED interface. Traffic flow is as follows:
Trusted device -> TRUSTED interface -> Firewall -> INTERNAL interface -> Internal device
Traffic is going IN to the trusted interface
VersionIPv4All my TRUSTED devices use IPv4
ProtocolTCPSSH uses TCP
Invert SourceNoSetting it to yes would say all sources but the selected source
SourceTRUSTED networkThe source is any device on the TRUSTED network. If you want a specific device or want to further specify the subnet, use Single host or Network, and specify from there.
Source PortanyDevices initiating a connection will use a random port
Invert DestinationNoSetting it to yes would say all destinations but the selected destination
DestinationINTERNAL networkThe destination is any device on the INTERNAL network. If you want a specific device or want to further specify the subnet, use Single host or Network, and specify from there.
Destination PortSSH (22)OPNsense has defaults for common ports, so you can select the SSH port. If you are not using a common port, you can select single port or range and specify the port or range of ports to use.
LogYesOptional and will use storage, but I would use it at least for deployment of this rule in case you need to troubleshoot
  1. Click Save
  2. Click Apply

With this, you should hopefully now understand how to create your own firewall rules.

Port-Forwarding

I wanted to cover one more topic that I believe is somewhat relevant to firewall ruling. Port-forwarding redirects inbound traffic to a specific device based on the chosen port. You will use this if you are deploying services to the internet, so that inbound traffic from the internet will be directed to the right spot. Make sure you are comfortable with your network security setup and have proper segmentation before port-forwarding! This is a risk as you create a point of entry to your network from the internet!

To create a port forward rule, using Plex as the example:

  1. Navigate to your OPNsense management GUI
  2. Navigate to Firewall | NAT | Destination NAT
  3. Click +
  4. Configure according to the table:
SettingOptionWhy
DisabledNoRule must be active to be enforced
CategoriesNothing selectedOptional OPNsense grouping for organization
DescriptionPlexOptional but highly recommended to give your rule a description so you know what it is for
InterfaceWANTraffic is inbound to the WAN interface
VersionIPv4Plex defaults to IPv4
ProtocolTCPPlex uses TCP for communication
Invert SourceNoEnabling it would say all but the selected source
Source AddressAnyAccessed by any address on the internet
Source PortAnyDevice will initiate connection with a random port
Invert DestinationNoEnabling it would say all but the selected destination
Destination AddressWAN addressThe traffic is going to your network, and therefore your OPNsense WAN address.
Destination Port32400The source is looking for port 32400 (Plex’s port)
Redirect Target IPSingle host or network <plex-server-ip>Select single host or network to specify where your Plex server is, then put the IP of your Plex server. OPNsense is redirecting the traffic to here.
Redirect Target Port32400If your Plex is using the default 32400 port, use this. Otherwise, specify the port you chose in setup if using a custom port, OPNsense will send it here
LogYesOptional and will use storage, but I would use it at least for deployment of this rule in case you need to troubleshoot
Firewall RuleRegister ruleRegister rule tells OPNsense to create a firewall rule for this traffic, which will show up in your firewall rules table. Optionally you can use Manual to create the firewall rule yourself. You can also use Pass, which will bypass the firewall and the NAT rule will allow it. I would recommend to not use Pass
  1. Click Save
  2. Click Apply

Closing Notes

This concludes the basic setup of my rebuild. From here you should be able to deploy VMs with Proxmox, and manage your OPNsense router with firewall rules. The next steps will include more advanced topics like a password vault with Vaultwarden, Unbound DNS, Traefik reverse proxy, Wireguard VPN, and web hosting. Some of these are managed through OPNsense plugins, while others can be managed through Docker, a containerization solution. My next guide will discuss Docker basics including installation, deployment, and configuration, as well as a docker Compose. I will also discuss Portainer, a centralized docker management platform in the guide after that.