What am I doing wrong with this rule?

Dear All,

It looks like I am doing something wrong but I do not see what/why. I hope someone can help.

I have an OpenWrt setup consisting of an OpenWrt router with Wifi and 3 network ports (WAN, LAN1, LAN2).

The WAN side is connected to a modem which is in bridge mode. The router gets a WAN IP address through the modem. Despite being in bridge mode the modem has a fixed IP (192.168.234.249) that allows access to its web interface.

I have several interfaces: i_Lan1, i_Lan2, i_Wan, i_Wifi_01, i_Wifi_02, i_Wifi_03

and several zones: z_WAN, z_Lan1, z_Wifi_01, z_Wifi_02, z_Wifi_03, z_Lan2

The intention is to allow access to the modem from the OpenWRT router and z_Lan1 ONLY.

To this end I added 2 rules before the rules that block access to all private IP ranges on Wan:

Adding the ‘Wan: Allow modem’ rule works as expected (can ping the modem from the router)

Adding the ‘Lan1: Allow modem’ rule works only if I use *’From: any zone’ (I can ping the modem and access its web interface, but access to the modem is too open / not according to my intention). Unexpected / My issue: The moment I use ‘From: Lan1’ the rule does NOT allow access to the modem web interface from Lan1.

What am I overlooking? Can anyone help?

Peter

If the modem is ipoe then 192.168.234.249 is treated as just another address on the Internet (only the modem will intercept it locally rather than forward it to the ISP). So without writing any rules at all, all zones that are allowed to use the Internet can also initiate a connection to the modem web page. If you don't want that, write blocking rules instead.

In order to really understand what is happening, we need some additional info:

  • what is the address for the modem?
  • please confirm that you do want z_Lan1 to access the modem
  • please confirm that it should be blocked from all others.

Please connect to your OpenWrt device using ssh and copy the output of the following commands and post it here using the "Preformatted text </> " button:
grafik
Remember to redact passwords, MAC addresses and any public IP addresses you may have:

ubus call system board
cat /etc/config/network
cat /etc/config/firewall

@mk24: There is a whole set of rules that block anything going out on Wan to 10.x.x.x, 172.16.x.x etc. I did not include all such details to keep things readable.

@psherman: You guys are very fast in responding. I just updated the original post to express the additional info. Once I have access (currently not at home) I will provide the output of the three commands.

Thats a bad idea as no one gets notifications on edits.

1 Like

Thank you for mentioning that, never thought about that there are no notifications on editing a topic

So here is the requested output showing the router details, the network config and the rules.
For a summary look at my original post.

root@Router:~# ubus call system board
{
        "kernel": "5.15.167",
        "hostname": "Router",
        "system": "MediaTek MT7620A ver:2 eco:6",
        "model": "Xiaomi MiWiFi Mini",
        "board_name": "xiaomi,miwifi-mini",
        "rootfs_type": "squashfs",
        "release": {
                "distribution": "OpenWrt",
                "version": "23.05.5",
                "revision": "r24106-10cc5fcd00",
                "target": "ramips/mt7620",
                "description": "OpenWrt 23.05.5 r24106-10cc5fcd00"
        }
}
root@Router:~# cat /etc/config/network

config interface 'loopback'
        option device 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fd46:cade:38e4::/48'

config switch
        option name 'switch0'
        option reset '1'
        option enable_vlan '1'

config switch_vlan
        option device 'switch0'
        option vlan '1'
        option ports '1 6t'
        option vid '1'
        option description 'Lan1: middle NW port'

config switch_vlan
        option device 'switch0'
        option vlan '2'
        option ports '0 6t'
        option vid '2'
        option description 'Lan2: NW port next USB'

config switch_vlan
        option device 'switch0'
        option vlan '3'
        option ports '4 6t'
        option vid '3'
        option description 'Wan: blue NW port'

config interface 'i_Wan'
        option proto 'dhcp'
        option device 'eth0.3'

config interface 'i_Lan1'
        option proto 'static'
        option device 'eth0.1'
        option ipaddr '192.168.240.193'
        option netmask '255.255.255.224'

config interface 'i_WiFi_01'
        option proto 'static'
        option device 'br_WiFi_01'
        option ipaddr '192.168.241.193'
        option netmask '255.255.255.224'

config device
        option type 'bridge'
        option name 'br_WiFi_01'
        option bridge_empty '1'
        option ipv6 '0'

config device
        option type 'bridge'
        option name 'br_WiFi_02'
        option bridge_empty '1'
        option ipv6 '0'

config interface 'i_WiFi_02'
        option proto 'static'
        option device 'br_WiFi_02'
        option ipaddr '192.168.242.193'
        option netmask '255.255.255.224'

config device
        option type 'bridge'
        option name 'br_WiFi_03'
        option bridge_empty '1'
        option ipv6 '0'

config interface 'i_WiFi_03'
        option proto 'static'
        option device 'br_WiFi_03'
        option ipaddr '192.168.243.193'
        option netmask '255.255.255.224'

config interface 'i_Lan2'
        option proto 'static'
        option device 'eth0.2'
        option ipaddr '192.168.244.193'
        option netmask '255.255.255.224'

root@Router:~# cat /etc/config/firewall

config defaults
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        option synflood_protect '1'

config zone
        option name 'z_Wan'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        option masq '1'
        option mtu_fix '1'
        list network 'i_Wan'

config rule
        option name 'This device: Reject IPv6 in'
        option family 'ipv6'
        list proto 'all'
        option src '*'
        option target 'REJECT'

config rule
        option name 'This device: Reject IPv6 out'
        option family 'ipv6'
        option dest '*'
        option target 'REJECT'
        list proto 'all'

config rule
        option name 'All Zones: Reject IPv6 forward'
        option src '*'
        option dest '*'
        option target 'REJECT'
        option family 'ipv6'
        list proto 'all'

config rule
        option name 'Wan: Allow DHCP inbound'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_Wan'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'

config rule
        option name 'Wan: Allow DHCP outbound'
        list proto 'udp'
        option src_port '67'
        option dest 'z_Wan'
        option dest_port '68'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Wan: Allow DNS outbound'
        option dest 'z_Wan'
        option dest_port '53'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Wan: Allow NTP outbound'
        list proto 'udp'
        option dest 'z_Wan'
        option dest_port '123'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Wan: Allow modem'
        option family 'ipv4'
        option dest 'z_Wan'
        list dest_ip '192.168.234.249'
        option target 'ACCEPT'
        list proto 'tcp'
        list proto 'icmp'

config rule
        option name 'Lan1: Allow modem'
        option family 'ipv4'
        option src '*'
        option dest 'z_Wan'
        list dest_ip '192.168.234.249'
        option target 'ACCEPT'
        list proto 'tcp'
        list proto 'icmp'

config rule
        option name 'Wifi_01: Allow modem'
        option family 'ipv4'
        list proto 'tcp'
        list proto 'icmp'
        option src 'z_WiFi_01'
        option dest 'z_Wan'
        list dest_ip '192.168.234.249'
        option target 'ACCEPT'
        option enabled '0'

config rule
        option name 'Wan: Reject RFC1918 etc'
        option dest 'z_Wan'
        list dest_ip '192.168.0.0/16'
        list dest_ip '172.16.0.0/12'
        list dest_ip '10.0.0.0/8'
        list dest_ip '224.0.0.0/4'
        option target 'REJECT'
        option family 'ipv4'
        list proto 'all'

config rule
        option name 'Wan: Reject RFC1918 etc'
        option src '*'
        option dest 'z_Wan'
        option target 'REJECT'
        option family 'ipv4'
        list proto 'all'
        list dest_ip '192.168.0.0/16'
        list dest_ip '172.16.0.0/12'
        list dest_ip '10.0.0.0/8'
        list dest_ip '224.0.0.0/4'

config rule
        option name 'Lan1: Allow DHCP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_Lan1'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'

config rule
        option name 'Lan1: Allow ping'
        list proto 'icmp'
        option src 'z_Lan1'
        list dest_ip '192.168.240.193'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Lan1: Allow DNS'
        option src 'z_Lan1'
        option dest_port '53'
        option target 'ACCEPT'
        option family 'ipv4'
        list dest_ip '192.168.240.193'

config rule
        option name 'Lan1: Allow NTP'
        list proto 'udp'
        option src 'z_Lan1'
        option dest_port '123'
        option target 'ACCEPT'
        option family 'ipv4'
        list dest_ip '192.168.240.193'

config rule
        option name 'Lan1: Allow HTTP (mgnt)'
        list proto 'tcp'
        option src 'z_Lan1'
        list dest_ip '192.168.240.193'
        option dest_port '80'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Lan1: Allow SSH (mgnt)'
        option family 'ipv4'
        list proto 'tcp'
        option src 'z_Lan1'
        list dest_ip '192.168.240.193'
        option dest_port '22'
        option target 'ACCEPT'

config zone
        option name 'z_Lan1'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        option family 'ipv4'
        list network 'i_Lan1'

config forwarding
        option src 'z_Lan1'
        option dest 'z_Wan'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'Lan1: DNS -> router'
        option family 'ipv4'
        option src 'z_Lan1'
        option src_dport '53'
        option dest_ip '192.168.240.193'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'Lan1: NTP -> router'
        list proto 'udp'
        option src 'z_Lan1'
        option src_dport '123'
        option dest_ip '192.168.240.193'

config zone
        option name 'z_WiFi_01'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        list network 'i_WiFi_01'

config forwarding
        option src 'z_WiFi_01'
        option dest 'z_Wan'

config rule
        option name 'WiFi_01: Allow DHCP'
        list proto 'udp'
        option src 'z_WiFi_01'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'WiFi_01: Allow ping'
        option family 'ipv4'
        list proto 'icmp'
        option src 'z_WiFi_01'
        list dest_ip '192.168.241.193'
        option target 'ACCEPT'

config rule
        option name 'Wifi_01: Allow DNS'
        option family 'ipv4'
        option src 'z_WiFi_01'
        option dest_port '53'
        option target 'ACCEPT'
        list dest_ip '192.168.241.193'

config rule
        option name 'WiFi_01: Allow NTP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_01'
        list dest_ip '192.168.241.193'
        option dest_port '123'
        option target 'ACCEPT'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_01: DNS -> Router'
        option family 'ipv4'
        option src 'z_WiFi_01'
        option src_dport '53'
        option dest_ip '192.168.241.193'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_01: NTP --> Router'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_01'
        option src_dport '123'
        option dest_ip '192.168.241.193'

config zone
        option name 'z_WiFi_02'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'ACCEPT'
        list network 'i_WiFi_02'

config forwarding
        option src 'z_WiFi_02'
        option dest 'z_Wan'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_02: DNS --> router'
        option family 'ipv4'
        option src 'z_WiFi_02'
        option src_dport '53'
        option dest_ip '192.168.242.193'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_02: NTP --> Router'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_02'
        option src_dport '123'
        option dest_ip '192.168.242.193'

config rule
        option name 'WiFi_01: Allow HTTP (mgmt)'
        option family 'ipv4'
        list proto 'tcp'
        option src 'z_WiFi_01'
        list dest_ip '192.168.241.193'
        option dest_port '80'
        option target 'ACCEPT'

config rule
        option name 'WiFi_01: Allow SSH (mgnt)'
        option family 'ipv4'
        list proto 'tcp'
        option src 'z_WiFi_01'
        list dest_ip '192.168.241.193'
        option dest_port '22'
        option target 'ACCEPT'

config rule
        option name 'WiFi_02: Allow DHCP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_02'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'

config rule
        option name 'WiFi_02: Allow ping'
        option family 'ipv4'
        list proto 'icmp'
        option src 'z_WiFi_02'
        list dest_ip '192.168.242.193'
        option target 'ACCEPT'

config rule
        option name 'WiFi_02: Allow DNS'
        option family 'ipv4'
        option src 'z_WiFi_02'
        list dest_ip '192.168.242.193'
        option dest_port '53'
        option target 'ACCEPT'

config rule
        option name 'WiFi_02: Allow NTP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_02'
        list dest_ip '192.168.242.193'
        option dest_port '123'
        option target 'ACCEPT'

config zone
        option name 'z_WiFi_03'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        list network 'i_WiFi_03'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_03: DNS --> Router'
        option family 'ipv4'
        option src 'z_WiFi_03'
        option src_dport '53'
        option dest_ip '192.168.243.193'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'WiFi_03: NTP --> Router'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_03'
        option src_dport '123'
        option dest_ip '192.168.243.193'

config rule
        option name 'WiFi_03: Allow DHCP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_03'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'

config rule
        option name 'WiFi_03: Allow ping'
        option family 'ipv4'
        list proto 'icmp'
        option src 'z_WiFi_03'
        list dest_ip '192.168.243.193'
        option target 'ACCEPT'

config rule
        option name 'WiFi_03: Allow DNS'
        option family 'ipv4'
        option src 'z_WiFi_03'
        list dest_ip '192.168.243.193'
        option dest_port '53'
        option target 'ACCEPT'

config rule
        option name 'WiFi_03: Allow NTP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_WiFi_03'
        list dest_ip '192.168.243.193'
        option dest_port '123'
        option target 'ACCEPT'

config zone
        option name 'z_Lan2'
        option input 'REJECT'
        option output 'ACCEPT'
        option forward 'REJECT'
        list network 'i_Lan2'

config forwarding
        option src 'z_Lan2'
        option dest 'z_Wan'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'Lan2: DNS --> router'
        option family 'ipv4'
        option src 'z_Lan2'
        option src_dport '53'
        option dest_ip '192.168.244.193'

config redirect
        option dest 'z_Wan'
        option target 'DNAT'
        option name 'Lan2: NTP --> Router'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_Lan2'
        option src_dport '123'
        option dest_ip '192.168.244.193'

config rule
        option name 'Lan2: Allow DHCP'
        option family 'ipv4'
        list proto 'udp'
        option src 'z_Lan2'
        option src_port '68'
        option dest_port '67'
        option target 'ACCEPT'

config rule
        option name 'Lan2: Allow ping'
        list proto 'icmp'
        option src 'z_Lan2'
        list dest_ip '192.168.244.193'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Lan2: Allow DNS'
        option family 'ipv4'
        option src 'z_Lan2'
        list dest_ip '192.168.244.193'
        option dest_port '53'
        option target 'ACCEPT'

config rule
        option name 'Lan2: Allow NTP'
        list proto 'udp'
        option src 'z_Lan2'
        list dest_ip '192.168.244.193'
        option dest_port '123'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Allow-IGMP'
        option src 'z_Wan'
        option proto 'igmp'
        option family 'ipv4'
        option target 'ACCEPT'
        option enabled '0'

config rule
        option name 'Lan1: Allow Lan2-AP'
        option src 'z_Lan1'
        option dest 'z_Lan2'
        list dest_ip '192.168.244.194'
        option target 'ACCEPT'
        option family 'ipv4'
        list proto 'tcp'
        list proto 'icmp'

config rule
        option name 'WiFi_01: Allow Lan2-AP'
        option src 'z_WiFi_01'
        option dest 'z_Lan2'
        list dest_ip '192.168.244.194'
        option target 'ACCEPT'
        option family 'ipv4'
        list proto 'tcp'
        list proto 'icmp'

I've skimmed through your configs, and honestly I am having trouble following it because you've basically re-written everything. It seems like you've essentially deleted all of the standard stuff and then made your own replacements. It also looks like you may have rules that are redundant and/or unnecessary. But it would require quite a bit of work to keep track of everything you've done.

Another really unusual thing you've done here is the use of .193 as your router's address on each of the networks. While there is no technical reason you can't use this, it's uncommon (most of the time it's at the top of bottom of the range, so on a /24, that would be the .1 or .254 address). That said, it is absolutely critical that the DHCP pool does not overlap the router's address. The default DHCP server config has the pool from .100-249, so hopefully yours is different.

All of that said, is there a reason that you've made your config so drastically different than the default (or common) use of OpenWrt?

2 Likes

When you are using any zone the rule is placed on top of the list.
When you specify a zone you have jump action to a more specific rule, like forward lan
Do the nft list ruleset to check for yourself.

1 Like

Hi Trendy,

I used the web interface only to configure the rules. I come from a different firewall (pf). Therefore when building these rules I tested each and every rule in an attempt to learn how OpenWRT works. I have always assumed:

  • The order in which the rules are processed are top to bottom
  • The moment a packet matches a rule the remaining rules are not processed.
    Your reply seems to indicate my assumption is incorrect.

I searched for a description of rules ordering but never found it. Maybe I use the wrong keywords.

Assuming I interpret your reply correctly (and as a result my assumption expressed above is not true):

  • I need to rethink my rules.
  • You would help me with a (pointer to) a description explaining the order etc rules are processed

Peter

so... coming back to this:

Would it be safe to say that, based on this:

... you were trying to recreate the firewall rules as expressed in pfsense?

If that's the reason for the significant re-write of your firewall, I would recommend that you reset to defaults.

From there, we can help you implement the specific goals you might have if you can provide clear descriptions of what you want to happen with each network.

Either add the source subnet in ‘Lan1: Allow modem’ and leave src zone any or you'll need to make a separate rule for every zone, allowing from lan1 and dropping from the others.

Hi psherman,

Yes, indeed I deleted the original config. The reason I did so was to start with a completely blocked firewall where I added rules starting from zero to get it to work in exactly the way I want it to. Unless I misunderstand how OpenWRT works there are no redundant rules. There are however quite a few similar rules as I never found a way to make rules apply to multiple but not all zones. For example: All my networks/zones (except WAN) have access to the OpenWRT DNS server. Is there any way to allow such acces in a single rule?

However from the reply of trendy I just might not understand how OpenWRT works exactly: My assumption on the order etc how rules are processed might be wrong. See my other reply.

In the relation to unusual networks 192.168.24x.193/26. I did this on purpose and yes the DHCP pools are configured accordingly for both dynamic and static addresses. I intend to use the OpenWRT config in multiple locations connecting to various sources, both with or without the (LTE) modem I mentioned. As a result the WAN network can basically get assigned any IP, both RFC1918 or a public IP address. The moment the assigned IP overlaps with any of 'my own' network ranges things do not work properly anymore. I already experienced such overlaps with the default OpenWRT private address and the more usual networks I selected myself before. I know my current networks do not guarantee anything but the current unusual addresses avoid collisions better.

I really wouldn't recommend that unless you have a complete understanding of OpenWrt's firewall and the function of each rule.

To be clear, the default OpenWrt firewall allows open/unrestricted routing from itself > wan and also from lan > wan (and obviously the established/related return traffic is allowed), but does not allow any unsolicited ingress to the router itself or to the network behind the router.

That is to say, the default state of the OpenWrt firewall is secure for the vast majority of use cases, and you're better off making small modifications if you want additional restrictions at that point.

There are no redundant rules in the system by default, but a user could obviously create two rules that do the same thing, or that are unnecessary. Those would be redundant in the context I was using. However, I think more to the point is that each network needs to be in a maximum of 1 zone... maybe that's what you're thinking about. You cannot put the same network into multiple zones.

Yes, in general you need to either create multiple accept (or reject) rules, 1 per zone, or create a main (any) accept (or reject) rule + rules to do the opposite for the zone that needs to be different.

There are other techniques, but the methods of constructing the firewall depend on the goals -- and if there is a complete vision of the rulesets, there are sometimes efficiencies to be gained with certain methods.

Rules are processed in order, and stop when they have the first hit (with the exception of the 'default' zone rule which is applied if there are no other conditions are met). But, I think you may not fully understand the way that OpenWrt's ZBF works, and this is even more of a reason to not try to re-write the whole thing... keep the defaults and make a few additions/modifications as needed.

You're possibly conflating address with subnet. I was talking about the 193 address itself -- vs just using the top or bottom of say a /24.

Yes, that's true. But you probably have a 'random' enough subnet to not worry about it... but if there is an overlap that happens (let's say the wan is 192.168.240.0/24), it's going to break one of your subnets anyway -- it would overlap your 192.168.2.193/26. This also wouldn't save you if the upstream was a large subnet such as an entire /16.

Sure... the default 192.168.1.1 is extremely common. So yes, use other subnets, but no need to use a /26 per-se.

But that's neither here nor there.