netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH nft] mergesort: find base value expression type via recursion
Date: Thu,  3 Sep 2020 13:41:03 +0200	[thread overview]
Message-ID: <20200903114103.6803-1-pablo@netfilter.org> (raw)

Sets that store flags might contain a mixture of values and binary
trees. Find the base value type via recursion to compare the
expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/mergesort.c                               | 30 +++++++++++++------
 tests/shell/testcases/sets/0055tcpflags_0     | 27 +++++++++++++++++
 .../testcases/sets/dumps/0055tcpflags_0.nft   | 10 +++++++
 3 files changed, 58 insertions(+), 9 deletions(-)
 create mode 100755 tests/shell/testcases/sets/0055tcpflags_0
 create mode 100644 tests/shell/testcases/sets/dumps/0055tcpflags_0.nft

diff --git a/src/mergesort.c b/src/mergesort.c
index 02094b486aeb..6494b19844fa 100644
--- a/src/mergesort.c
+++ b/src/mergesort.c
@@ -13,6 +13,21 @@
 
 static int expr_msort_cmp(const struct expr *e1, const struct expr *e2);
 
+static const struct expr *expr_msort_value(const struct expr *expr)
+{
+	switch (expr->etype) {
+	case EXPR_SET_ELEM:
+		return expr_msort_value(expr->key);
+	case EXPR_BINOP:
+	case EXPR_MAPPING:
+		return expr_msort_value(expr->left);
+	case EXPR_VALUE:
+		return expr;
+	default:
+		BUG("Unknown expression %s\n", expr_name(expr));
+	}
+}
+
 static int concat_expr_msort_cmp(const struct expr *e1, const struct expr *e2)
 {
 	struct list_head *l = (&e2->expressions)->next;
@@ -35,18 +50,15 @@ static int concat_expr_msort_cmp(const struct expr *e1, const struct expr *e2)
 static int expr_msort_cmp(const struct expr *e1, const struct expr *e2)
 {
 	switch (e1->etype) {
-	case EXPR_SET_ELEM:
-		return expr_msort_cmp(e1->key, e2->key);
-	case EXPR_VALUE:
-		return mpz_cmp(e1->value, e2->value);
 	case EXPR_CONCAT:
 		return concat_expr_msort_cmp(e1, e2);
-	case EXPR_MAPPING:
-		return expr_msort_cmp(e1->left, e2->left);
-	case EXPR_BINOP:
-		return expr_msort_cmp(e1->left, e2->left);
 	default:
-		BUG("Unknown expression %s\n", expr_name(e1));
+		e1 = expr_msort_value(e1);
+		e2 = expr_msort_value(e2);
+
+		assert(e1->etype == e2->etype && e1->etype == EXPR_VALUE);
+
+		return mpz_cmp(e1->value, e2->value);
 	}
 }
 
diff --git a/tests/shell/testcases/sets/0055tcpflags_0 b/tests/shell/testcases/sets/0055tcpflags_0
new file mode 100755
index 000000000000..a2b24eb2981b
--- /dev/null
+++ b/tests/shell/testcases/sets/0055tcpflags_0
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+EXPECTED="add table ip test
+
+add set ip test tcp_good_flags { type tcp_flag ; flags constant ; elements = {
+  ( 0 | 0 | 0 |ack| 0 | 0 ),  \
+  ( 0 | 0 | 0 |ack| 0 |urg),  \
+  ( 0 | 0 | 0 |ack|psh| 0 ),  \
+  ( 0 | 0 | 0 |ack|psh|urg),  \
+  ( 0 | 0 |rst| 0 | 0 | 0 ),  \
+  ( 0 | 0 |rst|ack| 0 | 0 ),  \
+  ( 0 | 0 |rst|ack| 0 |urg),  \
+  ( 0 | 0 |rst|ack|psh| 0 ),  \
+  ( 0 | 0 |rst|ack|psh|urg),  \
+  ( 0 |syn| 0 | 0 | 0 | 0 ),  \
+  ( 0 |syn| 0 |ack| 0 | 0 ),  \
+  ( 0 |syn| 0 |ack| 0 |urg),  \
+  ( 0 |syn| 0 |ack|psh| 0 ),  \
+  ( 0 |syn| 0 |ack|psh|urg),  \
+  (fin| 0 | 0 |ack| 0 | 0 ),  \
+  (fin| 0 | 0 |ack| 0 |urg),  \
+  (fin| 0 | 0 |ack|psh| 0 ),  \
+  (fin| 0 | 0 |ack|psh|urg)   \
+} ; }"
+
+set -e
+$NFT -f - <<< $EXPECTED
diff --git a/tests/shell/testcases/sets/dumps/0055tcpflags_0.nft b/tests/shell/testcases/sets/dumps/0055tcpflags_0.nft
new file mode 100644
index 000000000000..ffed5426577e
--- /dev/null
+++ b/tests/shell/testcases/sets/dumps/0055tcpflags_0.nft
@@ -0,0 +1,10 @@
+table ip test {
+	set tcp_good_flags {
+		type tcp_flag
+		flags constant
+		elements = { fin | psh | ack | urg, fin | psh | ack, fin | ack | urg, fin | ack, syn | psh | ack | urg,
+			     syn | psh | ack, syn | ack | urg, syn | ack, syn, rst | psh | ack | urg,
+			     rst | psh | ack, rst | ack | urg, rst | ack, rst, psh | ack | urg,
+			     psh | ack, ack | urg, ack }
+	}
+}
-- 
2.20.1


                 reply	other threads:[~2020-09-03 11:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200903114103.6803-1-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).