Skip to contentSkip to conversation

segtree: Fix get element for little endian ranges

PATCH In review Phil Sutter 2 participants 2 messages Nov 14, 2019 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH 1/1 segtree: Fix get element for little endian ranges

This fixes get element command for interval sets with host byte order data type, like e.g. mark. During serializing of the range (or element) to query, data was exported in wrong byteorder and consequently not found in kernel.

The mystery part is that code seemed correct: When calling constant_expr_alloc() from set_elem_add(), the set key's byteorder was passed with correct value of BYTEORDER_HOST_ENDIAN.

Comparison with delete/add element code paths though turned out that in those use-cases, constant_expr_alloc() is called with BYTEORDER_INVALID:

- seg_tree_init() takes byteorder field value of first element in init->expressions (i.e., the elements requested on command line) and assigns that to tree->byteorder
- tree->byteorder is passed to constant_expr_alloc() in set_insert_interval()
- the elements' byteorder happens to be the default value

This patch may not fix the right side, but at least it aligns get with add/delete element codes.

Fixes a43cc8d53096d ("src: support for get element command")
---
 src/segtree.c                                 | 12 +++---
 .../sets/0040get_host_endian_elements_0       | 43 +++++++++++++++++++
 2 files changed, 50 insertions(+), 5 deletions(-)
 create mode 100755 tests/shell/testcases/sets/0040get_host_endian_elements_0
src/segtree.c +7 -5
index 10c82eed5378f..9f1eecc0ae7e1 100644
@@ -622,12 +622,12 @@ int set_to_intervals(struct list_head *errs, struct set *set,
 }
 
 static void set_elem_add(const struct set *set, struct expr *init, mpz_t value,
-			 uint32_t flags)
+			 uint32_t flags, enum byteorder byteorder)
 {
 	struct expr *expr;
 
 	expr = constant_expr_alloc(&internal_location, set->key->dtype,
-				   set->key->byteorder, set->key->len, NULL);
+				   byteorder, set->key->len, NULL);
 	mpz_set(expr->value, value);
 	expr = set_elem_expr_alloc(&internal_location, expr);
 	expr->flags = flags;
@@ -649,14 +649,16 @@ struct expr *get_set_intervals(const struct set *set, const struct expr *init)
 	list_for_each_entry(i, &init->expressions, list) {
 		switch (i->key->etype) {
 		case EXPR_VALUE:
-			set_elem_add(set, new_init, i->key->value, i->flags);
+			set_elem_add(set, new_init, i->key->value,
+				     i->flags, i->byteorder);
 			break;
 		default:
 			range_expr_value_low(low, i);
-			set_elem_add(set, new_init, low, 0);
+			set_elem_add(set, new_init, low, 0, i->byteorder);
 			range_expr_value_high(high, i);
 			mpz_add_ui(high, high, 1);
-			set_elem_add(set, new_init, high, EXPR_F_INTERVAL_END);
+			set_elem_add(set, new_init, high,
+				     EXPR_F_INTERVAL_END, i->byteorder);
 			break;
 		}
 	}
tests/shell/testcases/sets/0040get_host_endian_elements_0 new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.24.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Phil Sutter

This fixes get element command for interval sets with host byte order data type, like e.g. mark. During serializing of the range (or element) to query, data was exported in wrong byteorder and consequently not found in kernel.

The mystery part is that code seemed correct: When calling constant_expr_alloc() from set_elem_add(), the set key's byteorder was passed with correct value of BYTEORDER_HOST_ENDIAN.

Comparison with delete/add element code paths though turned out that in those use-cases, constant_expr_alloc() is called with BYTEORDER_INVALID:

- seg_tree_init() takes byteorder field value of first element in init->expressions (i.e., the elements requested on command line) and assigns that to tree->byteorder
- tree->byteorder is passed to constant_expr_alloc() in set_insert_interval()
- the elements' byteorder happens to be the default value

Quoted 5 lines

This patch may not fix the right side, but at least it aligns get with add/delete element codes.

Fixes a43cc8d53096d ("src: support for get element command")