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.
| Property | Definition |
|---|---|
| Interface | The network interface the rule applies to, and provides the overall context for the rule. Typically WAN, LAN, or a VLAN interface. |
| Action | What 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 |
| Direction | A 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. |
| Version | The IP version the rule applies to, IPv4, IPv6, or Both |
| Protocol | The type of traffic the rule applies to, TCP, UDP, TCP/UDP, ICMP, Any, etc |
| Source | Where the traffic originates from, can be a single IP, a subnet, or a grouping of IPs |
| Source Port | The port that the traffic originates from. In most cases you will use Any as connecting clients pick a random port to initiate a connection. |
| Destination | Where the traffic is going, can be a single IP, a subnet, or a grouping of IPs |
| Destination Port | The port of the service being reached, like 443 for HTTPS or 22 for SSH |
Stateful vs. Stateless Firewalls
| Stateful Firewall | Stateless 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 automatically | Inspects 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:
- Navigate to your OPNsense management GUI
- Navigate to Firewall | Rules
- Click +
- Configure according to the table:
| Setting | Option | Why |
|---|---|---|
| Enabled | Yes | You must enable the firewall rule in order for it to be active |
| Categories | Nothing Selected | OPNsense grouping option, you may use it if you like |
| Description | TRUSTED SSH to INTERNAL | Descriptions are optional but highly recommended so you can quickly see the purpose of a rule you have |
| Interface (rule) | TRUSTED | Select the interface for which the traffic would be inbound. In this case, the traffic would be inbound to the TRUSTED VLAN interface |
| Interface (origin) | any | Origin is largely only relevant for outbound rules, keep it set to any |
| Quick | Yes | Try 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. |
| Action | Pass | Pass to allow the traffic. |
| Direction | In | Rule 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 |
| Version | IPv4 | All my TRUSTED devices use IPv4 |
| Protocol | TCP | SSH uses TCP |
| Invert Source | No | Setting it to yes would say all sources but the selected source |
| Source | TRUSTED network | The 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 Port | any | Devices initiating a connection will use a random port |
| Invert Destination | No | Setting it to yes would say all destinations but the selected destination |
| Destination | INTERNAL network | The 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 Port | SSH (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. |
| Log | Yes | Optional and will use storage, but I would use it at least for deployment of this rule in case you need to troubleshoot |
- Click Save
- 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:
- Navigate to your OPNsense management GUI
- Navigate to Firewall | NAT | Destination NAT
- Click +
- Configure according to the table:
| Setting | Option | Why |
|---|---|---|
| Disabled | No | Rule must be active to be enforced |
| Categories | Nothing selected | Optional OPNsense grouping for organization |
| Description | Plex | Optional but highly recommended to give your rule a description so you know what it is for |
| Interface | WAN | Traffic is inbound to the WAN interface |
| Version | IPv4 | Plex defaults to IPv4 |
| Protocol | TCP | Plex uses TCP for communication |
| Invert Source | No | Enabling it would say all but the selected source |
| Source Address | Any | Accessed by any address on the internet |
| Source Port | Any | Device will initiate connection with a random port |
| Invert Destination | No | Enabling it would say all but the selected destination |
| Destination Address | WAN address | The traffic is going to your network, and therefore your OPNsense WAN address. |
| Destination Port | 32400 | The source is looking for port 32400 (Plex’s port) |
| Redirect Target IP | Single 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 Port | 32400 | If 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 |
| Log | Yes | Optional and will use storage, but I would use it at least for deployment of this rule in case you need to troubleshoot |
| Firewall Rule | Register rule | Register 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 |
- Click Save
- 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.