firewall4 icon indicating copy to clipboard operation
firewall4 copied to clipboard

fw4: enable mac ranges for rule

Open ckorber opened this issue 5 months ago • 30 comments

nft supports handling mac ranges and therefore this commit changes fw4 to support that feature. The src_mac is now allowed to be a address with CIDR notation. If no CIDR notation is configured, the old logic is applied.

So this is now possible:

option src_mac '00:11:AA:BB:CC:DD-00:11:AA:FF:FF:FF'

Also negation with '!' at the beginning to block every MAC not in the range is possible.

ckorber avatar Oct 02 '25 11:10 ckorber

CIDR is wrong approach for MAC address, you have to be able to extract 2 youngest bits of oldest byte.

brada4 avatar Oct 02 '25 12:10 brada4

So if I understand you correctly you mean that it is not useful to enable the whole range, because the first byte contains important information that should not be manipulated, am I right?

I can modify it to only allow ranges in the last three youngest bytes (so not the oui). Is that also your intention?

ckorber avatar Oct 07 '25 06:10 ckorber

The grouping of MAC addresses with mask is very welcome, just that presuming CIDR is wrong. Yo uare on the right path, just that took a bit odd turn. DOC: https://en.wikipedia.org/wiki/MAC_address#Ranges_of_group_and_locally_administered_addresses

Example src 00:00:00:00:00:00/01:00:00:00:00:00 drop would drop unrealistic packets sourcing from multicast addresses.

brada4 avatar Oct 07 '25 07:10 brada4

I get what you mean, I think. The first byte holds valuable information that shouldn't be touched.

But if I limit it to bytes after the oui, we should be safe, right?

ckorber avatar Oct 07 '25 07:10 ckorber

Just use full mask, thats all I wanted to tell. There is no CIDR for mac addresses.

brada4 avatar Oct 07 '25 08:10 brada4

Something wrong with parentheses ;-)

brada4 avatar Oct 08 '25 19:10 brada4

Better? :)

I dropped the CIDR approach and let it parse for ranges that are set or return as usual. If the range is wrong (that is if the lower is on the right) it returns nothing.

~~EDIT: Oh I didn't push again.~~

ckorber avatar Oct 08 '25 20:10 ckorber

You can compile ucode and ./run_tests.sh (it is a bit tricky first time, you have to disable all or most extensions via cmake-gui or parameters)

brada4 avatar Oct 08 '25 20:10 brada4

You dont have to check special case with 0 or -1 mask that makes no-op-ish rules

brada4 avatar Oct 08 '25 20:10 brada4

Does that work for you @brada4 ?

ckorber avatar Oct 14 '25 07:10 ckorber

Wait for @jow- to examine it.

brada4 avatar Oct 14 '25 08:10 brada4

ping @jow-

ckorber avatar Oct 20 '25 06:10 ckorber

Advise you reword commit message: Also negation with '!' at the beginning to block every MAC not in the range is possible. to Also negation with '!' at the beginning to block every MAC in the range is possible.

systemcrash avatar Oct 29 '25 15:10 systemcrash

But I meant what I wrote? AFAIK the '!' negates the range and applies to the opposite, so it handles every MAC not in the range.

I could change it to: Also negation with '!' at the beginning to handle every MAC not in the range is possible.

ckorber avatar Oct 31 '25 06:10 ckorber

It is double no in your sentence meaning YES in some languages...

brada4 avatar Oct 31 '25 11:10 brada4

If a range is specified with '!', like '!00:11:AA:00:00:00-00:11:AA:FF:FF:FF'
it handles every MAC that is not within the range.

I rephrased it. Is it now clearer?

ckorber avatar Oct 31 '25 11:10 ckorber

Handle could be ambiguous depending on the action requested. The initial wording of ! == block (within the range) is clear and concrete. Less cognitive load.

systemcrash avatar Oct 31 '25 12:10 systemcrash

exclamation mark is called negation in iptables doc eg "negative matching using exclamation mark is supported"

brada4 avatar Oct 31 '25 14:10 brada4

@brada4 : I used your suggestion for the commit message.

Any other way to progress this PR? I think @jow- is not available at the moment.

ckorber avatar Nov 10 '25 11:11 ckorber

One potential pitfall with ambiguity: it becomes difficult to differentiate between MAC and hyphen separator when the MACs themselves use hyphen - between octets. :)

00-11-AA-00-00-00-00-11-AA-FF-FF-FF.

Although I can't think of a better separator. Hyphen is typically used to denote ranges. If this is deemed problematic, how about underscore? No, that's a binder. Tilde?

00-11-AA-00-00-00~00-11-AA-FF-FF-FF.

Thoughts @ckorber ?

systemcrash avatar Nov 10 '25 12:11 systemcrash

nftables takes all imaginable formats in

table inet t {
 chain c {
 ether saddr & 01:00:00:00:00:00 == 0x0
  ether saddr "02-00-00-00-00-00"-"04.ff.ff.ff.ff.ff"
 }
}

out

table inet t {
        chain c {
                ether saddr & 01:00:00:00:00:00 == 00:00:00:00:00:00
                ether saddr 02:00:00:00:00:00-04:ff:ff:ff:ff:ff
        }
}

0x0 in first line overflows if exceeding uint32 so it is not really useful for anything. standard format is with colon, other formats needs to be escaped.

brada4 avatar Nov 10 '25 13:11 brada4

Hate that I didn't see that :smile: It wouldn't be a problem in luci (as it is with my depending PR openwrt/luci#8017), because it relies on : as separator.

Maybe 00-01-AA-00-00-00..00-01-AA-FF-FF-FF gives the right idea? But I don't know if this is the best approach? Maybe changing this adds an additional cognitive load to remember that ranges are noted with .. as range symbol in fw4. I think - is more intuitive.

Additionally after parsing it transforms to 00:01:AA:00:00:00-00:01:AA:FF:FF:FF again in fw4.

ckorber avatar Nov 10 '25 13:11 ckorber

99% of users will supply valid values but we dont want others to break ruleset.

brada4 avatar Nov 10 '25 13:11 brada4

I think - is more intuitive.

Agreed. Just happier that we at least discussed the potential problem here.

systemcrash avatar Nov 10 '25 15:11 systemcrash

Don't want to push it, but there is no progress on this PR. Can someone review and merge it? Or is it only possible by @jow-?

ckorber avatar Nov 20 '25 08:11 ckorber

Any maintainer can.

systemcrash avatar Nov 20 '25 11:11 systemcrash

@ckorber Recommend tweaking the commit message a bit to e.g.

So this is now possible:

option src_mac '00:11:AA:00:00:00-00:11:AA:FF:FF:FF'

In addition to the original:

option src_mac '00:11:AA:00:00:00'

systemcrash avatar Nov 21 '25 14:11 systemcrash

I added that. Thanks!

@ckorber Recommend tweaking the commit message a bit to e.g.

So this is now possible:

option src_mac '00:11:AA:00:00:00-00:11:AA:FF:FF:FF'

In addition to the original:

option src_mac '00:11:AA:00:00:00'

ckorber avatar Nov 21 '25 19:11 ckorber

@nbd168 or @stintel : Do one of you want to review, because you committed to the project in the past? @jow- seems to be unavailable for review. Thanks in advance!

ckorber avatar Nov 26 '25 10:11 ckorber

Hi @robimarko could we get this in for 25?

systemcrash avatar Nov 26 '25 21:11 systemcrash