Blame SOURCES/0021-src-Add-support-for-concatenated-set-ranges.patch

911625
From 7b1f98e90a32865faca9a97f4348f20c753cd2f3 Mon Sep 17 00:00:00 2001
911625
From: Phil Sutter <psutter@redhat.com>
911625
Date: Fri, 14 Feb 2020 14:51:33 +0100
911625
Subject: [PATCH] src: Add support for concatenated set ranges
911625
911625
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1795224
911625
Upstream Status: nftables commit 8ac2f3b2fca38
911625
911625
commit 8ac2f3b2fca38b6533043b0678730c10ba4dc5ef
911625
Author: Stefano Brivio <sbrivio@redhat.com>
911625
Date:   Thu Jan 30 01:16:57 2020 +0100
911625
911625
    src: Add support for concatenated set ranges
911625
911625
    After exporting field lengths via NFTNL_SET_DESC_CONCAT attributes,
911625
    we now need to adjust parsing of user input and generation of
911625
    netlink key data to complete support for concatenation of set
911625
    ranges.
911625
911625
    Instead of using separate elements for start and end of a range,
911625
    denoting the end element by the NFT_SET_ELEM_INTERVAL_END flag,
911625
    as it's currently done for ranges without concatenation, we'll use
911625
    the new attribute NFTNL_SET_ELEM_KEY_END as suggested by Pablo. It
911625
    behaves in the same way as NFTNL_SET_ELEM_KEY, but it indicates
911625
    that the included key represents the upper bound of a range.
911625
911625
    For example, "packets with an IPv4 address between 192.0.2.0 and
911625
    192.0.2.42, with destination port between 22 and 25", needs to be
911625
    expressed as a single element with two keys:
911625
911625
      NFTA_SET_ELEM_KEY:            192.0.2.0 . 22
911625
      NFTA_SET_ELEM_KEY_END:        192.0.2.42 . 25
911625
911625
    To achieve this, we need to:
911625
911625
    - adjust the lexer rules to allow multiton expressions as elements
911625
      of a concatenation. As wildcards are not allowed (semantics would
911625
      be ambiguous), exclude wildcards expressions from the set of
911625
      possible multiton expressions, and allow them directly where
911625
      needed. Concatenations now admit prefixes and ranges
911625
911625
    - generate, for each element in a range concatenation, a second key
911625
      attribute, that includes the upper bound for the range
911625
911625
    - also expand prefixes and non-ranged values in the concatenation
911625
      to ranges: given a set with interval and concatenation support,
911625
      the kernel has no way to tell which elements are ranged, so they
911625
      all need to be. For example, 192.0.2.0 . 192.0.2.9 : 1024 is
911625
      sent as:
911625
911625
      NFTA_SET_ELEM_KEY:            192.0.2.0 . 1024
911625
      NFTA_SET_ELEM_KEY_END:        192.0.2.9 . 1024
911625
911625
    - aggregate ranges when elements received by the kernel represent
911625
      concatenated ranges, see concat_range_aggregate()
911625
911625
    - perform a few minor adjustments where interval expressions
911625
      are already handled: we have intervals in these sets, but
911625
      the set specification isn't just an interval, so we can't
911625
      just aggregate and deaggregate interval ranges linearly
911625
911625
    v4: No changes
911625
    v3:
911625
     - rework to use a separate key for closing element of range instead of
911625
       a separate element with EXPR_F_INTERVAL_END set (Pablo Neira Ayuso)
911625
    v2:
911625
     - reworked netlink_gen_concat_data(), moved loop body to a new function,
911625
       netlink_gen_concat_data_expr() (Phil Sutter)
911625
     - dropped repeated pattern in bison file, replaced by a new helper,
911625
       compound_expr_alloc_or_add() (Phil Sutter)
911625
     - added set_is_nonconcat_range() helper (Phil Sutter)
911625
     - in expr_evaluate_set(), we need to set NFT_SET_SUBKEY also on empty
911625
       sets where the set in the context already has the flag
911625
     - dropped additional 'end' parameter from netlink_gen_data(),
911625
       temporarily set EXPR_F_INTERVAL_END on expressions and use that from
911625
       netlink_gen_concat_data() to figure out we need to add the 'end'
911625
       element (Phil Sutter)
911625
     - replace range_mask_len() by a simplified version, as we don't need
911625
       to actually store the composing masks of a range (Phil Sutter)
911625
911625
    Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
911625
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
911625
---
911625
 include/expression.h |   1 +
8ff5ad
 include/rule.h       |   5 ++
8ff5ad
 src/evaluate.c       |   5 ++
8ff5ad
 src/netlink.c        | 109 +++++++++++++++++++++++++++++-----------
8ff5ad
 src/parser_bison.y   |  17 +++++--
8ff5ad
 src/rule.c           |  13 ++---
8ff5ad
 src/segtree.c        | 117 +++++++++++++++++++++++++++++++++++++++++++
911625
 7 files changed, 229 insertions(+), 38 deletions(-)
911625
911625
diff --git a/include/expression.h b/include/expression.h
911625
index ee726aa..2e41aa0 100644
911625
--- a/include/expression.h
911625
+++ b/include/expression.h
911625
@@ -460,6 +460,7 @@ extern int set_to_intervals(struct list_head *msgs, struct set *set,
911625
 			    struct expr *init, bool add,
911625
 			    unsigned int debug_mask, bool merge,
911625
 			    struct output_ctx *octx);
911625
+extern void concat_range_aggregate(struct expr *set);
911625
 extern void interval_map_decompose(struct expr *set);
911625
 
911625
 extern struct expr *get_set_intervals(const struct set *set,
911625
diff --git a/include/rule.h b/include/rule.h
911625
index c03b0b8..626973e 100644
911625
--- a/include/rule.h
911625
+++ b/include/rule.h
911625
@@ -372,6 +372,11 @@ static inline bool set_is_interval(uint32_t set_flags)
911625
 	return set_flags & NFT_SET_INTERVAL;
911625
 }
911625
 
911625
+static inline bool set_is_non_concat_range(struct set *s)
911625
+{
911625
+	return (s->flags & NFT_SET_INTERVAL) && s->desc.field_count <= 1;
911625
+}
911625
+
911625
 #include <statement.h>
911625
 
911625
 struct counter {
911625
diff --git a/src/evaluate.c b/src/evaluate.c
911625
index 58f458d..0c84816 100644
911625
--- a/src/evaluate.c
911625
+++ b/src/evaluate.c
911625
@@ -136,6 +136,11 @@ static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
911625
 
911625
 	if ((*expr)->byteorder == byteorder)
911625
 		return 0;
911625
+
911625
+	/* Conversion for EXPR_CONCAT is handled for single composing ranges */
911625
+	if ((*expr)->etype == EXPR_CONCAT)
911625
+		return 0;
911625
+
911625
 	if (expr_basetype(*expr)->type != TYPE_INTEGER)
911625
 		return expr_error(ctx->msgs, *expr,
911625
 			 	  "Byteorder mismatch: expected %s, got %s",
911625
diff --git a/src/netlink.c b/src/netlink.c
911625
index 83d863c..e0ba903 100644
911625
--- a/src/netlink.c
911625
+++ b/src/netlink.c
911625
@@ -98,10 +98,11 @@ struct nftnl_expr *alloc_nft_expr(const char *name)
911625
 static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
911625
 						  const struct expr *expr)
911625
 {
911625
-	const struct expr *elem, *key, *data;
911625
+	const struct expr *elem, *data;
911625
 	struct nftnl_set_elem *nlse;
911625
 	struct nft_data_linearize nld;
911625
 	struct nftnl_udata_buf *udbuf = NULL;
911625
+	struct expr *key;
911625
 
911625
 	nlse = nftnl_set_elem_alloc();
911625
 	if (nlse == NULL)
911625
@@ -119,6 +120,16 @@ static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
911625
 
911625
 	netlink_gen_data(key, &nld);
911625
 	nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_KEY, &nld.value, nld.len);
911625
+
911625
+	if (set->set_flags & NFT_SET_INTERVAL && expr->key->field_count > 1) {
911625
+		key->flags |= EXPR_F_INTERVAL_END;
911625
+		netlink_gen_data(key, &nld);
911625
+		key->flags &= ~EXPR_F_INTERVAL_END;
911625
+
911625
+		nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_KEY_END, &nld.value,
911625
+				   nld.len);
911625
+	}
911625
+
911625
 	if (elem->timeout)
911625
 		nftnl_set_elem_set_u64(nlse, NFTNL_SET_ELEM_TIMEOUT,
911625
 				       elem->timeout);
911625
@@ -186,28 +197,58 @@ void netlink_gen_raw_data(const mpz_t value, enum byteorder byteorder,
911625
 	data->len = len;
911625
 }
911625
 
911625
+static int netlink_export_pad(unsigned char *data, const mpz_t v,
911625
+			      const struct expr *i)
911625
+{
911625
+	mpz_export_data(data, v, i->byteorder,
911625
+			div_round_up(i->len, BITS_PER_BYTE));
911625
+
911625
+	return netlink_padded_len(i->len) / BITS_PER_BYTE;
911625
+}
911625
+
911625
+static int netlink_gen_concat_data_expr(int end, const struct expr *i,
911625
+					unsigned char *data)
911625
+{
911625
+	switch (i->etype) {
911625
+	case EXPR_RANGE:
911625
+		i = end ? i->right : i->left;
911625
+		break;
911625
+	case EXPR_PREFIX:
911625
+		if (end) {
911625
+			int count;
911625
+			mpz_t v;
911625
+
911625
+			mpz_init_bitmask(v, i->len - i->prefix_len);
911625
+			mpz_add(v, i->prefix->value, v);
911625
+			count = netlink_export_pad(data, v, i);
911625
+			mpz_clear(v);
911625
+			return count;
911625
+		}
911625
+		return netlink_export_pad(data, i->prefix->value, i);
911625
+	case EXPR_VALUE:
911625
+		break;
911625
+	default:
911625
+		BUG("invalid expression type '%s' in set", expr_ops(i)->name);
911625
+	}
911625
+
911625
+	return netlink_export_pad(data, i->value, i);
911625
+}
911625
+
911625
 static void netlink_gen_concat_data(const struct expr *expr,
911625
 				    struct nft_data_linearize *nld)
911625
 {
911625
+	unsigned int len = expr->len / BITS_PER_BYTE, offset = 0;
911625
+	int end = expr->flags & EXPR_F_INTERVAL_END;
911625
+	unsigned char data[len];
911625
 	const struct expr *i;
911625
-	unsigned int len, offset;
911625
-
911625
-	len = expr->len / BITS_PER_BYTE;
911625
-	if (1) {
911625
-		unsigned char data[len];
911625
-
911625
-		memset(data, 0, sizeof(data));
911625
-		offset = 0;
911625
-		list_for_each_entry(i, &expr->expressions, list) {
911625
-			assert(i->etype == EXPR_VALUE);
911625
-			mpz_export_data(data + offset, i->value, i->byteorder,
911625
-					div_round_up(i->len, BITS_PER_BYTE));
911625
-			offset += netlink_padded_len(i->len) / BITS_PER_BYTE;
911625
-		}
911625
 
911625
-		memcpy(nld->value, data, len);
911625
-		nld->len = len;
911625
-	}
911625
+	memset(data, 0, len);
911625
+
911625
+	list_for_each_entry(i, &expr->expressions, list)
911625
+		offset += netlink_gen_concat_data_expr(end, i, data + offset);
911625
+
911625
+	memcpy(nld->value, data, len);
911625
+	nld->len = len;
911625
 }
911625
 
911625
 static void netlink_gen_constant_data(const struct expr *expr,
911625
@@ -812,6 +853,7 @@ int netlink_delinearize_setelem(struct nftnl_set_elem *nlse,
911625
 	if (nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_FLAGS))
911625
 		flags = nftnl_set_elem_get_u32(nlse, NFTNL_SET_ELEM_FLAGS);
911625
 
911625
+key_end:
911625
 	key = netlink_alloc_value(&netlink_location, &nld);
911625
 	datatype_set(key, set->key->dtype);
911625
 	key->byteorder	= set->key->byteorder;
911625
@@ -880,6 +922,15 @@ int netlink_delinearize_setelem(struct nftnl_set_elem *nlse,
911625
 	}
911625
 out:
911625
 	compound_expr_add(set->init, expr);
911625
+
911625
+	if (!(flags & NFT_SET_ELEM_INTERVAL_END) &&
911625
+	    nftnl_set_elem_is_set(nlse, NFTNL_SET_ELEM_KEY_END)) {
911625
+		flags |= NFT_SET_ELEM_INTERVAL_END;
911625
+		nld.value = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_KEY_END,
911625
+					       &nld.len);
911625
+		goto key_end;
911625
+	}
911625
+
911625
 	return 0;
911625
 }
911625
 
911625
@@ -918,15 +969,16 @@ int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,
911625
 	set->init = set_expr_alloc(&internal_location, set);
911625
 	nftnl_set_elem_foreach(nls, list_setelem_cb, ctx);
911625
 
911625
-	if (!(set->flags & NFT_SET_INTERVAL))
911625
+	if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
911625
+		concat_range_aggregate(set->init);
911625
+	else if (set->flags & NFT_SET_INTERVAL)
911625
+		interval_map_decompose(set->init);
911625
+	else
911625
 		list_expr_sort(&ctx->set->init->expressions);
911625
 
911625
 	nftnl_set_free(nls);
911625
 	ctx->set = NULL;
911625
 
911625
-	if (set->flags & NFT_SET_INTERVAL)
911625
-		interval_map_decompose(set->init);
911625
-
911625
 	return 0;
911625
 }
911625
 
911625
@@ -935,6 +987,7 @@ int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,
911625
 			struct set *set, struct expr *init)
911625
 {
911625
 	struct nftnl_set *nls, *nls_out = NULL;
911625
+	int err = 0;
911625
 
911625
 	nls = nftnl_set_alloc();
911625
 	if (nls == NULL)
911625
@@ -958,18 +1011,18 @@ int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,
911625
 	set->init = set_expr_alloc(loc, set);
911625
 	nftnl_set_elem_foreach(nls_out, list_setelem_cb, ctx);
911625
 
911625
-	if (!(set->flags & NFT_SET_INTERVAL))
911625
+	if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
911625
+		concat_range_aggregate(set->init);
911625
+	else if (set->flags & NFT_SET_INTERVAL)
911625
+		err = get_set_decompose(table, set);
911625
+	else
911625
 		list_expr_sort(&ctx->set->init->expressions);
911625
 
911625
 	nftnl_set_free(nls);
911625
 	nftnl_set_free(nls_out);
911625
 	ctx->set = NULL;
911625
 
911625
-	if (set->flags & NFT_SET_INTERVAL &&
911625
-	    get_set_decompose(table, set) < 0)
911625
-		return -1;
911625
-
911625
-	return 0;
911625
+	return err;
911625
 }
911625
 
911625
 void netlink_dump_obj(struct nftnl_obj *nln, struct netlink_ctx *ctx)
911625
diff --git a/src/parser_bison.y b/src/parser_bison.y
911625
index 0fd9b94..ea83f52 100644
911625
--- a/src/parser_bison.y
911625
+++ b/src/parser_bison.y
911625
@@ -3551,7 +3551,6 @@ range_rhs_expr		:	basic_rhs_expr	DASH	basic_rhs_expr
911625
 
911625
 multiton_rhs_expr	:	prefix_rhs_expr
911625
 			|	range_rhs_expr
911625
-			|	wildcard_expr
911625
 			;
911625
 
911625
 map_expr		:	concat_expr	MAP	rhs_expr
911625
@@ -3645,7 +3644,7 @@ set_elem_option		:	TIMEOUT			time_spec
911625
 			;
911625
 
911625
 set_lhs_expr		:	concat_rhs_expr
911625
-			|	multiton_rhs_expr
911625
+			|	wildcard_expr
911625
 			;
911625
 
911625
 set_rhs_expr		:	concat_rhs_expr
911625
@@ -3898,7 +3897,7 @@ list_rhs_expr		:	basic_rhs_expr		COMMA		basic_rhs_expr
911625
 			;
911625
 
911625
 rhs_expr		:	concat_rhs_expr		{ $$ = $1; }
911625
-			|	multiton_rhs_expr	{ $$ = $1; }
911625
+			|	wildcard_expr		{ $$ = $1; }
911625
 			|	set_expr		{ $$ = $1; }
911625
 			|	set_ref_symbol_expr	{ $$ = $1; }
911625
 			;
911625
@@ -3939,7 +3938,17 @@ basic_rhs_expr		:	inclusive_or_rhs_expr
911625
 			;
911625
 
911625
 concat_rhs_expr		:	basic_rhs_expr
911625
-			|	concat_rhs_expr	DOT	basic_rhs_expr
911625
+			|	multiton_rhs_expr
911625
+			|	concat_rhs_expr		DOT	multiton_rhs_expr
911625
+			{
911625
+				struct location rhs[] = {
911625
+					[1]	= @2,
911625
+					[2]	= @3,
911625
+				};
911625
+
911625
+				$$ = handle_concat_expr(&@$, $$, $1, $3, rhs);
911625
+			}
911625
+			|	concat_rhs_expr		DOT	basic_rhs_expr
911625
 			{
911625
 				struct location rhs[] = {
911625
 					[1]	= @2,
911625
diff --git a/src/rule.c b/src/rule.c
911625
index 4669577..e18237b 100644
911625
--- a/src/rule.c
911625
+++ b/src/rule.c
911625
@@ -1512,7 +1512,8 @@ static int __do_add_setelems(struct netlink_ctx *ctx, struct set *set,
911625
 		return -1;
911625
 
911625
 	if (set->init != NULL &&
911625
-	    set->flags & NFT_SET_INTERVAL) {
911625
+	    set->flags & NFT_SET_INTERVAL &&
911625
+	    set->desc.field_count <= 1) {
911625
 		interval_map_decompose(expr);
911625
 		list_splice_tail_init(&expr->expressions, &set->init->expressions);
911625
 		set->init->size += expr->size;
911625
@@ -1533,7 +1534,7 @@ static int do_add_setelems(struct netlink_ctx *ctx, struct cmd *cmd,
911625
 	table = table_lookup(h, &ctx->nft->cache);
911625
 	set = set_lookup(table, h->set.name);
911625
 
911625
-	if (set->flags & NFT_SET_INTERVAL &&
911625
+	if (set_is_non_concat_range(set) &&
911625
 	    set_to_intervals(ctx->msgs, set, init, true,
911625
 			     ctx->nft->debug_mask, set->automerge,
911625
 			     &ctx->nft->output) < 0)
911625
@@ -1548,7 +1549,7 @@ static int do_add_set(struct netlink_ctx *ctx, const struct cmd *cmd,
911625
 	struct set *set = cmd->set;
911625
 
911625
 	if (set->init != NULL) {
911625
-		if (set->flags & NFT_SET_INTERVAL &&
911625
+		if (set_is_non_concat_range(set) &&
911625
 		    set_to_intervals(ctx->msgs, set, set->init, true,
911625
 				     ctx->nft->debug_mask, set->automerge,
911625
 				     &ctx->nft->output) < 0)
911625
@@ -1634,7 +1635,7 @@ static int do_delete_setelems(struct netlink_ctx *ctx, struct cmd *cmd)
911625
 	table = table_lookup(h, &ctx->nft->cache);
911625
 	set = set_lookup(table, h->set.name);
911625
 
911625
-	if (set->flags & NFT_SET_INTERVAL &&
911625
+	if (set_is_non_concat_range(set) &&
911625
 	    set_to_intervals(ctx->msgs, set, expr, false,
911625
 			     ctx->nft->debug_mask, set->automerge,
911625
 			     &ctx->nft->output) < 0)
911625
@@ -2488,7 +2489,7 @@ static int do_get_setelems(struct netlink_ctx *ctx, struct cmd *cmd,
911625
 	set = set_lookup(table, cmd->handle.set.name);
911625
 
911625
 	/* Create a list of elements based of what we got from command line. */
911625
-	if (set->flags & NFT_SET_INTERVAL)
911625
+	if (set_is_non_concat_range(set))
911625
 		init = get_set_intervals(set, cmd->expr);
911625
 	else
911625
 		init = cmd->expr;
911625
@@ -2501,7 +2502,7 @@ static int do_get_setelems(struct netlink_ctx *ctx, struct cmd *cmd,
911625
 	if (err >= 0)
911625
 		__do_list_set(ctx, cmd, table, new_set);
911625
 
911625
-	if (set->flags & NFT_SET_INTERVAL)
911625
+	if (set_is_non_concat_range(set))
911625
 		expr_free(init);
911625
 
911625
 	set_free(new_set);
911625
diff --git a/src/segtree.c b/src/segtree.c
911625
index 7217dbc..e859f84 100644
911625
--- a/src/segtree.c
911625
+++ b/src/segtree.c
911625
@@ -652,6 +652,11 @@ struct expr *get_set_intervals(const struct set *set, const struct expr *init)
911625
 			set_elem_add(set, new_init, i->key->value,
911625
 				     i->flags, i->byteorder);
911625
 			break;
911625
+		case EXPR_CONCAT:
911625
+			compound_expr_add(new_init, expr_clone(i));
911625
+			i->flags |= EXPR_F_INTERVAL_END;
911625
+			compound_expr_add(new_init, expr_clone(i));
911625
+			break;
911625
 		default:
911625
 			range_expr_value_low(low, i);
911625
 			set_elem_add(set, new_init, low, 0, i->byteorder);
911625
@@ -823,6 +828,9 @@ static int expr_value_cmp(const void *p1, const void *p2)
911625
 	struct expr *e2 = *(void * const *)p2;
911625
 	int ret;
911625
 
911625
+	if (expr_value(e1)->etype == EXPR_CONCAT)
911625
+		return -1;
911625
+
911625
 	ret = mpz_cmp(expr_value(e1)->value, expr_value(e2)->value);
911625
 	if (ret == 0) {
911625
 		if (e1->flags & EXPR_F_INTERVAL_END)
911625
@@ -834,6 +842,115 @@ static int expr_value_cmp(const void *p1, const void *p2)
911625
 	return ret;
911625
 }
911625
 
911625
+/* Given start and end elements of a range, check if it can be represented as
911625
+ * a single netmask, and if so, how long, by returning zero or a positive value.
911625
+ */
911625
+static int range_mask_len(const mpz_t start, const mpz_t end, unsigned int len)
911625
+{
911625
+	mpz_t tmp_start, tmp_end;
911625
+	int ret;
911625
+
911625
+	mpz_init_set_ui(tmp_start, mpz_get_ui(start));
911625
+	mpz_init_set_ui(tmp_end, mpz_get_ui(end));
911625
+
911625
+	while (mpz_cmp(tmp_start, tmp_end) <= 0 &&
911625
+		!mpz_tstbit(tmp_start, 0) && mpz_tstbit(tmp_end, 0) &&
911625
+		len--) {
911625
+		mpz_fdiv_q_2exp(tmp_start, tmp_start, 1);
911625
+		mpz_fdiv_q_2exp(tmp_end, tmp_end, 1);
911625
+	}
911625
+
911625
+	ret = !mpz_cmp(tmp_start, tmp_end) ? (int)len : -1;
911625
+
911625
+	mpz_clear(tmp_start);
911625
+	mpz_clear(tmp_end);
911625
+
911625
+	return ret;
911625
+}
911625
+
911625
+/* Given a set with two elements (start and end), transform them into a
911625
+ * concatenation of ranges. That is, from a list of start expressions and a list
911625
+ * of end expressions, form a list of start - end expressions.
911625
+ */
911625
+void concat_range_aggregate(struct expr *set)
911625
+{
911625
+	struct expr *i, *start = NULL, *end, *r1, *r2, *next, *r1_next, *tmp;
911625
+	struct list_head *r2_next;
911625
+	int prefix_len, free_r1;
911625
+	mpz_t range, p;
911625
+
911625
+	list_for_each_entry_safe(i, next, &set->expressions, list) {
911625
+		if (!start) {
911625
+			start = i;
911625
+			continue;
911625
+		}
911625
+		end = i;
911625
+
911625
+		/* Walk over r1 (start expression) and r2 (end) in parallel,
911625
+		 * form ranges between corresponding r1 and r2 expressions,
911625
+		 * store them by replacing r2 expressions, and free r1
911625
+		 * expressions.
911625
+		 */
911625
+		r2 = list_first_entry(&expr_value(end)->expressions,
911625
+				      struct expr, list);
911625
+		list_for_each_entry_safe(r1, r1_next,
911625
+					 &expr_value(start)->expressions,
911625
+					 list) {
911625
+			mpz_init(range);
911625
+			mpz_init(p);
911625
+
911625
+			r2_next = r2->list.next;
911625
+			free_r1 = 0;
911625
+
911625
+			if (!mpz_cmp(r1->value, r2->value)) {
911625
+				free_r1 = 1;
911625
+				goto next;
911625
+			}
911625
+
911625
+			mpz_sub(range, r2->value, r1->value);
911625
+			mpz_sub_ui(range, range, 1);
911625
+			mpz_and(p, r1->value, range);
911625
+
911625
+			/* Check if we are forced, or if it's anyway preferable,
911625
+			 * to express the range as two points instead of a
911625
+			 * netmask.
911625
+			 */
911625
+			prefix_len = range_mask_len(r1->value, r2->value,
911625
+						    r1->len);
911625
+			if (prefix_len < 0 ||
911625
+			    !(r1->dtype->flags & DTYPE_F_PREFIX)) {
911625
+				tmp = range_expr_alloc(&r1->location, r1,
911625
+						       r2);
911625
+
911625
+				list_replace(&r2->list, &tmp->list);
911625
+				r2_next = tmp->list.next;
911625
+			} else {
911625
+				tmp = prefix_expr_alloc(&r1->location, r1,
911625
+							prefix_len);
911625
+				tmp->len = r2->len;
911625
+
911625
+				list_replace(&r2->list, &tmp->list);
911625
+				r2_next = tmp->list.next;
911625
+				expr_free(r2);
911625
+			}
911625
+
911625
+next:
911625
+			mpz_clear(p);
911625
+			mpz_clear(range);
911625
+
911625
+			r2 = list_entry(r2_next, typeof(*r2), list);
911625
+			compound_expr_remove(start, r1);
911625
+
911625
+			if (free_r1)
911625
+				expr_free(r1);
911625
+		}
911625
+
911625
+		compound_expr_remove(set, start);
911625
+		expr_free(start);
911625
+		start = NULL;
911625
+	}
911625
+}
911625
+
911625
 void interval_map_decompose(struct expr *set)
911625
 {
911625
 	struct expr **elements, **ranges;
911625
-- 
8ff5ad
2.31.1
911625