From OpenWrt 22.03 onward, firewall4 (fw4) uses nftables as its default backend while retaining the familiar UCI syntax in /etc/config/firewall. UCI covers a reasonable subset of Netfilter; special nftables behavior should use the include mechanisms instead of forcing everything into one config rule.1
Warning: Back up
/etc/config/firewallbefore editing. Double-check the result before reloading; if access is lost, use Failsafe to restore it. Never expose an Internet port just because a rule looks plausible.1
Manage it with LuCI and UCI
In LuCI, open Network → Firewall. LuCI maps closely to the file sections but may remove # comments. From the CLI, uci changes values in /etc/config/firewall; uci commit saves them, but the firewall still needs a reload to generate new tables. The source warns that uci does not understand content inside /etc/firewall.user.1
Create a rule with UCI:
| |
Inspect what UCI created:
| |
To see parsing diagnostics while reloading, use /etc/init.d/firewall reload or fw4 reload and read the complete output before closing the SSH session.1
Core configuration sections
defaults
Defaults define the global policy and common protections:
| |
zone
A zone groups one or more interfaces. A WAN zone commonly enables masquerading and mtu_fix:
| |
input is traffic reaching the router through the zone, output originates on the router, and forward crosses interfaces. masq applies IPv4 NAT to traffic leaving a zone and is normally used on WAN.1
forwarding
Forwarding is directional; it is not automatically bidirectional:
| |
To allow traffic both ways, create two forwardings with reversed src and dest, and ensure that connection tracking is appropriate on at least one side, normally the zone with masq.1
rule
A rule can match zones, IPs, MAC addresses, ports, protocols, families, and a target:
| |
An example that accepts SSH from a specific LAN range to a server:
| |
With only src, a rule matches traffic entering the router; with only dest, traffic leaving it; with both, forwarded traffic. src_ip and dest_ip accept an address or CIDR, port ranges use start-stop, and proto and family must match the actual traffic.1
redirect: DNAT and SNAT
DNAT sends incoming traffic to an internal host:
| |
SNAT changes the source of traffic leaving a zone:
| |
For DNAT, specify src_dport when the rule must not expose every port matching the remaining conditions. Test from the Internet and restrict the source whenever possible.1
IP sets
An IP set matches a list of addresses instead of repeating many rules. Distinguish fw3 and fw4 options, the IP-set type, storage/match options, and how a rule or redirect refers to ipset. Do not reference a set that has not been created; when a package creates it automatically, check startup ordering and firewall logs.1
nftables includes on fw4 (22.03+)
UCI firewall does not cover every Netfilter feature. With fw4, either include an nftables snippet at a table/chain position or include a compatible shell script. A chain snippet example:
| |
A table-post include:
| |
For /etc/firewall.user, declare a script include and mark it fw4-compatible:
| |
fw4 can also load automatic drop-in includes below /usr/share/nftables.d/ when auto_includes is enabled. Packages may use that hook; do not put arbitrary code there without checking ordering and chain scope.1
fw3 includes and reload procedure
On OpenWrt 21.02 and earlier, type 'script' and /etc/firewall.user are the older fw3 path; do not carry a fw3 example into fw4 without checking nftables syntax. A minimal fw3 include from the source is:
| |
After each change:
- Inspect the diff of
/etc/config/firewalland every include file. - Run
uci commit firewallafter UCI edits. - Reload and read the diagnostics.
- Test LuCI/SSH, LAN-to-WAN traffic, zone isolation, and published ports.
- If access is lost, enter Failsafe and restore the backup.
A firewall is a contract among zones, forwardings, rules, redirects, and includes. A successful build does not prove that the policy is safe; test each traffic direction in practice.1


Góp Ý / Bình Luận / Đánh giá