From 0545e0c13b3b7dff4dd53c8a68d8d1066c2829c0 Mon Sep 17 00:00:00 2001 From: Patrick McHardy Date: Mon, 17 Feb 2014 14:06:44 +0000 Subject: netlink: fix prefix expression handling The prefix expression handling is full of bugs: - netlink_gen_data() is used to construct the prefix mask from the full prefix expression. This is both conceptually wrong, the prefix expression is *not* data, and buggy, it only assumes network masks and thus only handles big endian types. - Prefix expression reconstruction doesn't check whether the mask is a valid prefix and reconstructs crap otherwise. It doesn't reconstruct prefixes for anything but network addresses. On top of that its needlessly complicated, using the mpz values directly its a simple matter of finding the sequence of 1's that extend up to the full width. - Unnecessary cloning of expressions where a simple refcount increase would suffice. Rewrite that code properly. Signed-off-by: Patrick McHardy --- src/netlink_linearize.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/netlink_linearize.c') diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c index e5fb536b..9d59374c 100644 --- a/src/netlink_linearize.c +++ b/src/netlink_linearize.c @@ -193,9 +193,14 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx, netlink_gen_expr(ctx, expr->left, sreg); if (expr->right->ops->type == EXPR_PREFIX) { - right = expr->right->prefix; + mpz_t mask; + + mpz_init(mask); + mpz_prefixmask(mask, expr->right->len, expr->right->prefix_len); + netlink_gen_raw_data(mask, expr->right->byteorder, + expr->right->len / BITS_PER_BYTE, &nld); + mpz_clear(mask); - netlink_gen_data(expr->right, &nld); zero.len = nld.len; nle = alloc_nft_expr("bitwise"); @@ -205,6 +210,8 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx, nft_rule_expr_set(nle, NFT_EXPR_BITWISE_MASK, &nld.value, nld.len); nft_rule_expr_set(nle, NFT_EXPR_BITWISE_XOR, &zero.value, zero.len); nft_rule_add_expr(ctx->nlr, nle); + + right = expr->right->prefix; } else { right = expr->right; } -- cgit v1.2.3