luci icon indicating copy to clipboard operation
luci copied to clipboard

Firewall Custom Rules not always applied

Open OhmEye opened this issue 7 years ago • 2 comments

I'm using a TPLink A7 with "Powered by LuCI Master (git-18.356.64384-31d259a) / OpenWrt SNAPSHOT r8859-f36bc3f"

In LUCI Network>Firewall>Custom Rules I have some rules to insert logging at the top of some chains. This works as expected and clicking the Restart Firewall button on that page does work as intended.

However, applying changes on other Network pages may result in the IPTables chains being rebuilt without the custom rules as if the /etc/firewall.user include is ignored. Here are some steps to reproduce:

  1. In LUCI enter into Network>Firewall>Custom Rules any valid rule such as: "iptables -I zone_wan_forward 1 -p tcp -m conntrack --ctstate NEW -m limit --limit 60/sec -j LOG --log-level info --log-prefix "ctstate=new "
  2. Click "Restart Firewall" button
  3. In CLI shell enter: "iptables -L"
  4. Note the custom LOG rule is present in the zone_wan_forward chain
  5. In LUCI Network>Hostnames add a hostname for a local host (or make any change that can be applied)
  6. In CLI shell enter "iptables -L"
  7. Note the LOG rule is now missing from the zone_wan_forward chain
  8. At this point you can either do "service firewall restart" in CLI or click "Restart Firewall" on the Custom Rules page, and check "iptables -L" again to see that the LOG rule is back.

Apparently there are many cases where "Save & Apply" in LUCI will cause the custom rules to be omitted from the chains. Adding a hostname is just an example, the same issue occurs when I apply other settings under the Network tab. After applying other changes I have to explicitly restart the firewall to make sure my custom rules get reapplied.

OhmEye avatar Dec 30 '18 16:12 OhmEye

The firewall.user file is only processed on firewall restarts, not reloads. The problem is that your custom rules utilize internal firewall chains which get flushed and rebuilt on reloads, this is why they're getting dropped.

Use the forwarding_wan_rule chain instead to avoid this behavior. Alternatively set option reload 1 for the config include section of /etc/config/firewall and make sure that your custom rule commands to not create duplicates.

Refer to https://oldwiki.archive.openwrt.org/doc/uci/firewall#packet_flow for usable chains. Rules referencing or being contained in one of the chains marked system or user are left alone, rules placed inside internal chains or rules jumping to internal chains are getting deleted on firewall reloads.

jow- avatar Dec 30 '18 18:12 jow-

Thanks for the explanation. I actually have custom rules for several chains, including the zone_wan_forward chain that contains forwarding_wan_rule. My intent is to log new TCP sessions in each zone context, so I'm inserting a log rule at the top of each appropriate chain and it works as I want. I've added the option 1 reload as you suggested and it now does exactly what I wanted.

OhmEye avatar Dec 30 '18 21:12 OhmEye