OpenWrt firewall mystery

The mistake in your thinking is that IP addresses on different interfaces of the router should be treated differently on the firewall. This is not the case as all of them are considered the same and treated with the INPUT policy. Come to think about it, it doesn't make any sense to differentiate one IP from another, as they all belong to the same device. If you allow connections on one IP, there is no sense to deny on another.

This is why the guides suggest to use REJECT or DROP for the INPUT in untrusted zones, like wan or guest.

2 Likes

Here I beg to differ. You would often NOT want the configuration interface of a device to be available from any connected network, whilst still wanting to allow other services to be available to all connected networks.

I mean, just look at that from a distance:

root@tplink1:/etc/config# ncat -v -l 192.168.2.1 12345
Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Listening on 192.168.2.1:12345
Ncat: Connection from 192.168.7.130.
Ncat: Connection from 192.168.7.130:34892.

And that with a configuration that does not allow zone forwarding from the 192.168.7 network to the 192.168.2 network.

From your explanations I understand now why this happens, and that considering the algorithm given by Thinkcat in Recommended firewall settings - #2 by lleachii, the packet from 192.168.7.130 to 192.168.2.1 does not go OUT through another interface but is rather destined as INPUT to the router. But its still not what one would expect at first glance ...

So the solution is to REJECT all INPUT to the router in all zones but the private zone in general, and then to individually ALLOW access by devices in other zones/LANs than private to exactly the IP address of the router IN THAT zone/LAN.

Can that be expressed by one simple rule per zone/LAN?

Kind regards,
Sebastian

There is no such term in linux. If some vendor isolates one interface or IP to be accessed from specific interface/IPs, then this is because of firewall rules in place, not by design.

You are asking the wrong question. The right question is "Does guest zone need access to the Luci?"
Most likely not. Therefore it is advised to deny all input from guest and all other untrusted zones and open the necessary protocols, like dhcp and dns.

3 Likes

There is no such thing as this in Linux either. If you were accessing the device from another enumerated zone, then that zone's INPUT rule applied to the traffic, not the guest setting.

If you are saying specific IPs only...yes that's possible; but differed from your original discussion of configuring a dedicated "management port".

Although, I could be confused because I donno what the phrase "other zones/LANs than private" means, since that rule is ACCEPT already; or perhaps the OP still misunderstands the firewall's INPUT setting...it's hard to determine...

Yeah, that's what I learned today. If I configure e.g. mosquitto like:

listener 1883 192.168.2.1
listener 1883 192.168.5.1
listener 1883 192.168.6.1
listener 1883 192.168.7.1

such that mosquitto does NOT listen on 192.168.4.1, then this does not prevent a device in 192.168.4 to simply access mosquitto via e.g. 192.168.2 or 192.168.5. Totally unexpected, at least for me. And many thanks for telling me about that.

Not sure I'm asking the wrong question.

Does guest zone need access to Luci? Indeed no. That's why I configured Luci to listen on 192.168.2.1 only, not knowing that that doesn't prevent access from devices in other LANs even when forwarding is not granted.

"To deny all input from guest and all other untrusted zones and open the necessary protocols, like dhcp and dns." Can that be expressed by one simple rule per zone/LAN, without listing all protocols individually? I think this is a good question. I would like to have a stable set of firewall rules even if I'm modifying the services available on the router itself.

So I'd like to express that IP traffic arriving from any LAN and destined to the router itself should be rejected if the destination IP is one of the router's IP addresses in a different LAN than from where the packet arrives (different LAN excluding broadcast addresses).

How can this be done with the smallest set of rules?

Kind regards,
Sebastian

You have INPUT on the private network set to ACCEPT.

Because it's INPUT, not FORWARD. You even linked where I note this:

You can do that, but as I told you before it is pointless.
Configure input policy DROP/REJECT, then add a rule to allow input from zone A towards the IP of the interface in zone A.

3 Likes

Yes, I know now. Because it's INPUT and I wrongly configured INPUT ACCEPT. And in addition because Linux has a weak host model; if it had a strong host model access would not be granted.

???

:confused:

I think Imma leave this thread alone on firewall. I hope the best for correcting your firewall config. I was never taught to imagine my configs or machines to a better state - I programmed/configured them to do so.

Yes, I know that I have to configure INPUT DROP/REJECT to avoid that crosstalk, and will do that.

But how do I express that rule to nevertheless grant input from zone A towards the routers IP in zone A best? I'm not familiar with those rules at all ...

Kind regards,
Sebastian

It's not crosstalk which to me implies FORWARDing - it's INPUT (per the actual design of the networking stack). I hope you understand this. because there's more network configs "under the hood" that may throw you for a "loop" (pun intended).

But if I read your question correctly...you simply make that zone's INPUT rule ACCEPT...so I must be confused regarding your question.

1 Like

Something like this:

uci add firewall rule
uci set firewall.@rule[-1].dest_port='80'
uci set firewall.@rule[-1].src='guest'
uci set firewall.@rule[-1].name='test http'
uci set firewall.@rule[-1].family='ipv4'
uci add_list firewall.@rule[-1].dest_ip='172.17.17.1'
uci set firewall.@rule[-1].target='ACCEPT'
uci add_list firewall.@rule[-1].proto='tcp'
uci commit firewall
service firewall restart
3 Likes

I think I understand, but who knows? What I understand is that the firewall treats IP packets from a given zone addressed to "the router" via the zones INPUT rule, IP packets addressed to another LAN within the same zone via the zones FORWARD rule, and IP packets addressed to another zone via the zones "FORWARDING" rules; and that IP packets addressed to an IP address of "the router" even when those IP addresses are in a different zone are handled by the firewall via the INPUT rule not the "FORWARDING" rules.

Would the following be sufficient:

  1. INPUT REJECT for every zone

  2. For every zone that should be allowed to access router services:

uci add firewall rule
uci set firewall.@rule[-1].src='br-guest'
uci set firewall.@rule[-1].name='router access from guest'
uci set firewall.@rule[-1].family='ipv4'
uci add_list firewall.@rule[-1].dest_ip='192.168.5.1'
uci set firewall.@rule[-1].target='ACCEPT'
uci commit firewall
service firewall restart

(of course adapting src, and the dest_ip for every zone to the IP address of the router in that zone)?

Kind regards,
Sebastian

:+1:

Although, I'm still not sure why you logically consider and separate this differently...it's not like the machine magically becomes a different machine because it has another IP. I can only imagine that you're trying to isolate a service on the router from the private VLAN - other than the web GUI... otherwise, I don't understand the effort; but yes, @trendy's rule will work to only allow access to the preferred DST IP. Please note, some users have subsequently locked themselves out of configuring their router by adding such rules.

I will admit the INPUT thing somewhat baffled me when I first learned iptables.

The web GUI is not the only service needed for a LAN usually (i.e. DHCP, DNS, NTP).

3 Likes

That is correct.

I wouldn't recommend it for the lan zone. You might get locked out.

This is the zone name, not the interface name. You are also not using ports or protocols, so this rule is basically reverting the drop on input policy. Yes, it will be limiting the input to just the interface IP, in case you are running multiple instances of the same server.

2 Likes

I didn't realize the OP removed the port...and changed the zone name...good catch!

Now, this really makes me think we don't understand the OP's use case.

The paradigm you are trying to implement doesn't work well for OpenWrt.
There are protocols like DHCP which cannot work properly if you limit the destination address scope in the firewall rule and restrict everything else.
Moreover, using interface/socket binding is unreliable due to possible race conditions which procd has no means to resolve.

2 Likes

Dear gentle people, it seems to work!

Now I can't access Luci's uhttpd from any zone but private anymore, but I can still access the mosquitto service on the router whereever mosquitto is listening on the zone's IP address of the router, plus any other service the router might offer on those IP addresses (DHCP, DNS, ...):

root@tplink1:/etc/config# lsof -ni
COMMAND     PID      USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ntpd       1502      root    3u  IPv6   1966      0t0  UDP *:ntp
uhttpd     1673      root    3u  IPv4   2272      0t0  TCP 192.168.2.1:www (LISTEN)
odhcp6c    1725      root    4u  IPv6   2379      0t0  UDP *:dhcpv6-client
dropbear   1889      root    3u  IPv4   2510      0t0  TCP 192.168.2.1:ssh (LISTEN)
dropbear  21579      root    5u  IPv4 198214      0t0  TCP 192.168.2.1:ssh->192.168.2.190:49904 (ESTABLISHED)
dropbear  21579      root    7u  IPv4 198221      0t0  TCP 127.0.0.1:6010 (LISTEN)
dropbear  21580      root    5u  IPv4 198218      0t0  TCP 192.168.2.1:ssh->192.168.2.190:49905 (ESTABLISHED)
dnsmasq   24781   dnsmasq    4u  IPv4 216070      0t0  UDP *:bootps
dnsmasq   24781   dnsmasq    6u  IPv4 216073      0t0  UDP *:domain
dnsmasq   24781   dnsmasq    7u  IPv4 216074      0t0  TCP *:domain (LISTEN)
dnsmasq   24781   dnsmasq    8u  IPv6 216075      0t0  UDP *:domain
dnsmasq   24781   dnsmasq    9u  IPv6 216076      0t0  TCP *:domain (LISTEN)
mosquitto 25197 mosquitto    4u  IPv4 217603      0t0  TCP 192.168.2.1:1883 (LISTEN)
mosquitto 25197 mosquitto    5u  IPv4 217604      0t0  TCP 192.168.5.1:1883 (LISTEN)
mosquitto 25197 mosquitto    6u  IPv4 217605      0t0  TCP 192.168.6.1:1883 (LISTEN)
mosquitto 25197 mosquitto    7u  IPv4 217606      0t0  TCP 192.168.7.1:1883 (LISTEN)
mosquitto 25197 mosquitto    9u  IPv4 217611      0t0  TCP 192.168.6.1:1883->192.168.6.181:60451 (ESTABLISHED)
mosquitto 25197 mosquitto   10u  IPv4 219624      0t0  TCP 192.168.7.1:1883->192.168.7.130:35718 (ESTABLISHED)
mosquitto 25197 mosquitto   11u  IPv4 217820      0t0  TCP 192.168.5.1:1883->192.168.5.200:38118 (ESTABLISHED)
nginx     25429      root    6u  IPv4 219600      0t0  TCP 192.168.2.1:1880 (LISTEN)
nginx     25429      root    7u  IPv4 219601      0t0  TCP 192.168.5.1:1880 (LISTEN)
nginx     25429      root    8u  IPv4 219602      0t0  TCP 192.168.6.1:1880 (LISTEN)
nginx     25429      root    9u  IPv4 219603      0t0  TCP 192.168.7.1:1880 (LISTEN)
nginx     25436    nobody    6u  IPv4 219600      0t0  TCP 192.168.2.1:1880 (LISTEN)
nginx     25436    nobody    7u  IPv4 219601      0t0  TCP 192.168.5.1:1880 (LISTEN)
nginx     25436    nobody    8u  IPv4 219602      0t0  TCP 192.168.6.1:1880 (LISTEN)
nginx     25436    nobody    9u  IPv4 219603      0t0  TCP 192.168.7.1:1880 (LISTEN)
dropbear  26110      root    5u  IPv4 223427      0t0  TCP 192.168.2.1:ssh->192.168.2.190:53968 (ESTABLISHED)
dropbear  26110      root    7u  IPv4 223433      0t0  TCP 127.0.0.1:6011 (LISTEN)
dropbear  26111      root    5u  IPv4 223430      0t0  TCP 192.168.2.1:ssh->192.168.2.190:53969 (ESTABLISHED)
dropbear  26395      root    5u  IPv4 224131      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54040 (ESTABLISHED)
dropbear  26395      root    7u  IPv4 224137      0t0  TCP 127.0.0.1:6012 (LISTEN)
dropbear  26396      root    5u  IPv4 224134      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54041 (ESTABLISHED)
dropbear  26427      root    5u  IPv4 224340      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54161 (ESTABLISHED)
dropbear  26427      root    7u  IPv4 224346      0t0  TCP 127.0.0.1:6013 (LISTEN)
dropbear  26428      root    5u  IPv4 224343      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54163 (ESTABLISHED)
dropbear  26450      root    5u  IPv4 224399      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54167 (ESTABLISHED)
dropbear  26450      root    7u  IPv4 224405      0t0  TCP 127.0.0.1:6014 (LISTEN)
dropbear  26451      root    5u  IPv4 224402      0t0  TCP 192.168.2.1:ssh->192.168.2.190:54168 (ESTABLISHED)

Many thanks, also for clarifying the background of all of this (at least to the depth of fw3).

Kind regards,
Sebastian

I guess I still have to allow broadcast IP addresses to enter the router (e.g. 192.168.5.255, 255.255.255.255)?

I don't understand. Can you clarify that?

Kind regards,
Sebastian

The service may fail to start if the interface is not ready.
Restarting the interface can make the service inoperable.
Among the affected services are Dropbear, uHTTPd and others.

1 Like