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_delinearize.c | 52 +++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) (limited to 'src/netlink_delinearize.c') diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c index 645bf63e..0e75c8a9 100644 --- a/src/netlink_delinearize.c +++ b/src/netlink_delinearize.c @@ -663,33 +663,27 @@ static void meta_match_postprocess(struct rule_pp_ctx *ctx, } } -static int expr_value2cidr(struct expr *expr) +/* Convert a bitmask to a prefix length */ +static unsigned int expr_mask_to_prefix(struct expr *expr) { - int i, j, k = 0; - uint32_t data[4], bits; - uint32_t len = div_round_up(expr->len, BITS_PER_BYTE) / sizeof(uint32_t); + unsigned long n; - assert(expr->ops->type == EXPR_VALUE); - - mpz_export_data(data, expr->value, expr->byteorder, len); - - for (i = len - 1; i >= 0; i--) { - j = 32; - bits = UINT32_MAX >> 1; - - if (data[i] == UINT32_MAX) - goto next; + n = mpz_scan1(expr->value, 0); + return mpz_scan0(expr->value, n + 1) - n; +} - while (--j >= 0) { - if (data[i] == bits) - break; +/* Return true if a bitmask can be expressed as a prefix length */ +static bool expr_mask_is_prefix(const struct expr *expr) +{ + unsigned long n1, n2; - bits >>=1; - } -next: - k += j; - } - return k; + n1 = mpz_scan1(expr->value, 0); + if (n1 == ULONG_MAX) + return false; + n2 = mpz_scan0(expr->value, n1 + 1); + if (n2 < expr->len || n2 == ULONG_MAX) + return false; + return true; } /* Convert a series of inclusive OR expressions into a list */ @@ -729,13 +723,13 @@ static void relational_binop_postprocess(struct expr *expr) expr->op = OP_FLAGCMP; expr_free(binop); - } else if ((binop->left->dtype->type == TYPE_IPADDR || - binop->left->dtype->type == TYPE_IP6ADDR) && - binop->op == OP_AND) { - expr->left = expr_clone(binop->left); + } else if (binop->left->dtype->flags & DTYPE_F_PREFIX && + binop->op == OP_AND && + expr_mask_is_prefix(binop->right)) { + expr->left = expr_get(binop->left); expr->right = prefix_expr_alloc(&expr->location, - expr_clone(value), - expr_value2cidr(binop->right)); + expr_get(value), + expr_mask_to_prefix(binop->right)); expr_free(value); expr_free(binop); } -- cgit v1.2.3