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@xxxxxxxxxxxxx> --- 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