Skip to contentSkip to conversation

datatype: accept abbrevs and ignore case on parsing symbolic constants

RFC PATCH In review Jo-Philipp Wich 2 participants 2 messages Apr 11, 2022 – Apr 12, 2022 netfilter

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 datatype: accept abbrevs and ignore case on parsing symbolic constants

Currently nftables does not accept abbreviated or lowercased weekday names as claimed in the nftables wiki [1]. This is due to the fact that symbolic_constant_parse() performs a strict equality check of the given constant value against the list of potential choices.

In order to implement the behaviour described by the wiki - which seems useful and intuitive in general - adjust the constant parsing function to to perform a case-insensitive prefix match of the user supplied value against the choice list.

The modified code does not check uniqueness of the prefix value, it will simply return the first matching item, but it will ensure to reject an empty string value.

1: https://wiki.nftables.org/wiki-nftables/index.php/Matching_packet_metainformation#Matching_by_time

---
 src/datatype.c              |  5 +++--
 tests/py/any/meta.t         |  4 ++++
 tests/py/any/meta.t.payload | 18 ++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
src/datatype.c +3 -2
index 2e31c858..ce4a8aa8 100644
@@ -149,9 +149,10 @@ struct error_record *symbolic_constant_parse(struct parse_ctx *ctx,
 	const struct symbolic_constant *s;
 	const struct datatype *dtype;
 	struct error_record *erec;
+	size_t idlen;
 
-	for (s = tbl->symbols; s->identifier != NULL; s++) {
-		if (!strcmp(sym->identifier, s->identifier))
+	for (s = tbl->symbols, idlen = strlen(sym->identifier); s->identifier != NULL; s++) {
+		if (idlen > 0 && !strncasecmp(sym->identifier, s->identifier, idlen))
 			break;
 	}
 
tests/py/any/meta.t +4 -0
index 12fabb79..4f130e7d 100644
@@ -212,7 +212,11 @@ meta time < "2022-07-01 11:00:00" accept;ok
 meta time > "2022-07-01 11:00:00" accept;ok
 meta day "Saturday" drop;ok
 meta day 6 drop;ok;meta day "Saturday" drop
+meta day "saturday" drop;ok;meta day "Saturday" drop
+meta day "Sat" drop;ok;meta day "Saturday" drop
+meta day "sat" drop;ok;meta day "Saturday" drop
 meta day "Satturday" drop;fail
+meta day "" drop;fail
 meta hour "17:00" drop;ok
 meta hour "17:00:00" drop;ok;meta hour "17:00" drop
 meta hour "17:00:01" drop;ok
tests/py/any/meta.t.payload +18 -0
index 16dc1211..b43c43c4 100644
@@ -1023,6 +1023,24 @@ ip test-ip4 input
   [ cmp eq reg 1 0x00000006 ]
   [ immediate reg 0 drop ]
 
+# meta day "saturday" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
+# meta day "Sat" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
+# meta day "sat" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
 # meta day 6 drop
 ip test-ip4 input
   [ meta load day => reg 1 ]
Signature · 1 line
2.35.1
#1 lore raw

Hi,

Quoted 28 lines · Jo-Philipp Wich

Currently nftables does not accept abbreviated or lowercased weekday names as claimed in the nftables wiki [1]. This is due to the fact that symbolic_constant_parse() performs a strict equality check of the given constant value against the list of potential choices.

In order to implement the behaviour described by the wiki - which seems useful and intuitive in general - adjust the constant parsing function to to perform a case-insensitive prefix match of the user supplied value against the choice list.

The modified code does not check uniqueness of the prefix value, it will simply return the first matching item, but it will ensure to reject an empty string value.

1: https://wiki.nftables.org/wiki-nftables/index.php/Matching_packet_metainformation#Matching_by_time

---
 src/datatype.c              |  5 +++--
 tests/py/any/meta.t         |  4 ++++
 tests/py/any/meta.t.payload | 18 ++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
src/datatype.c +0 -0
index 2e31c858..ce4a8aa8 100644

src/datatype.c struct error_record *symbolic_constant_parse(struct parse_ctx *ctx, · line 154

Quoted 7 lines
src/datatype.c +1 -2
@@ -149,9 +149,10 @@ struct error_record *symbolic_constant_parse(struct parse_ctx *ctx,
 	const struct symbolic_constant *s;
 	const struct datatype *dtype;
 	struct error_record *erec;
+	size_t idlen;
 
-	for (s = tbl->symbols; s->identifier != NULL; s++) {
-		if (!strcmp(sym->identifier, s->identifier))

I'd suggest to add a flag for this:

DTYPE_F_ICASE

and set it on for the time datatype, I'd prefer to narrow down this feature to this specific case and extend it to more usecases progressively.

Would you send another patch version?

Thanks!

Quoted 53 lines
src/datatype.c +2 -0
+	for (s = tbl->symbols, idlen = strlen(sym->identifier); s->identifier != NULL; s++) {
+		if (idlen > 0 && !strncasecmp(sym->identifier, s->identifier, idlen))
 			break;
 	}
 
tests/py/any/meta.t +4 -0
index 12fabb79..4f130e7d 100644
@@ -212,7 +212,11 @@ meta time < "2022-07-01 11:00:00" accept;ok
 meta time > "2022-07-01 11:00:00" accept;ok
 meta day "Saturday" drop;ok
 meta day 6 drop;ok;meta day "Saturday" drop
+meta day "saturday" drop;ok;meta day "Saturday" drop
+meta day "Sat" drop;ok;meta day "Saturday" drop
+meta day "sat" drop;ok;meta day "Saturday" drop
 meta day "Satturday" drop;fail
+meta day "" drop;fail
 meta hour "17:00" drop;ok
 meta hour "17:00:00" drop;ok;meta hour "17:00" drop
 meta hour "17:00:01" drop;ok
tests/py/any/meta.t.payload +18 -0
index 16dc1211..b43c43c4 100644
@@ -1023,6 +1023,24 @@ ip test-ip4 input
   [ cmp eq reg 1 0x00000006 ]
   [ immediate reg 0 drop ]
 
+# meta day "saturday" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
+# meta day "Sat" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
+# meta day "sat" drop
+ip test-ip4 input
+  [ meta load day => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ immediate reg 0 drop ]
+
 # meta day 6 drop
 ip test-ip4 input
   [ meta load day => reg 1 ]
Signature · 1 line
2.35.1