All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] datatype: accept abbrevs and ignore case on parsing symbolic constants
@ 2022-04-11  8:08 Jo-Philipp Wich
  2022-04-12 22:11 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Jo-Philipp Wich @ 2022-04-11  8:08 UTC (permalink / raw)
  To: netfilter-devel; +Cc: 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

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
---
 src/datatype.c              |  5 +++--
 tests/py/any/meta.t         |  4 ++++
 tests/py/any/meta.t.payload | 18 ++++++++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/datatype.c b/src/datatype.c
index 2e31c858..ce4a8aa8 100644
--- a/src/datatype.c
+++ b/src/datatype.c
@@ -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;
 	}
 
diff --git a/tests/py/any/meta.t b/tests/py/any/meta.t
index 12fabb79..4f130e7d 100644
--- a/tests/py/any/meta.t
+++ b/tests/py/any/meta.t
@@ -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
diff --git a/tests/py/any/meta.t.payload b/tests/py/any/meta.t.payload
index 16dc1211..b43c43c4 100644
--- a/tests/py/any/meta.t.payload
+++ b/tests/py/any/meta.t.payload
@@ -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 ]
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH] datatype: accept abbrevs and ignore case on parsing symbolic constants
  2022-04-11  8:08 [RFC PATCH] datatype: accept abbrevs and ignore case on parsing symbolic constants Jo-Philipp Wich
@ 2022-04-12 22:11 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-04-12 22:11 UTC (permalink / raw)
  To: Jo-Philipp Wich; +Cc: netfilter-devel

Hi,

On Mon, Apr 11, 2022 at 10:08:22AM +0200, Jo-Philipp Wich wrote:
> 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
> 
> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
> ---
>  src/datatype.c              |  5 +++--
>  tests/py/any/meta.t         |  4 ++++
>  tests/py/any/meta.t.payload | 18 ++++++++++++++++++
>  3 files changed, 25 insertions(+), 2 deletions(-)
> 
> diff --git a/src/datatype.c b/src/datatype.c
> index 2e31c858..ce4a8aa8 100644
> --- a/src/datatype.c
> +++ b/src/datatype.c
> @@ -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!

> +	for (s = tbl->symbols, idlen = strlen(sym->identifier); s->identifier != NULL; s++) {
> +		if (idlen > 0 && !strncasecmp(sym->identifier, s->identifier, idlen))
>  			break;
>  	}
>  
> diff --git a/tests/py/any/meta.t b/tests/py/any/meta.t
> index 12fabb79..4f130e7d 100644
> --- a/tests/py/any/meta.t
> +++ b/tests/py/any/meta.t
> @@ -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
> diff --git a/tests/py/any/meta.t.payload b/tests/py/any/meta.t.payload
> index 16dc1211..b43c43c4 100644
> --- a/tests/py/any/meta.t.payload
> +++ b/tests/py/any/meta.t.payload
> @@ -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 ]
> -- 
> 2.35.1
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-12 23:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-11  8:08 [RFC PATCH] datatype: accept abbrevs and ignore case on parsing symbolic constants Jo-Philipp Wich
2022-04-12 22:11 ` Pablo Neira Ayuso

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.