Move the contents of the two major cases (I and II) into separate functions. Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- src/netlink_delinearize.c | 192 +++++++++++++++++++++----------------- 1 file changed, 107 insertions(+), 85 deletions(-) diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c index 4e5d64ede8bd..e8e9e5719ee8 100644 --- a/src/netlink_delinearize.c +++ b/src/netlink_delinearize.c @@ -2514,6 +2514,109 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop) } } +static bool stmt_payload_binop_postprocess_i(struct rule_pp_ctx *ctx) +{ + struct expr *expr, *binop, *payload, *value, *mask; + struct stmt *stmt = ctx->stmt; + mpz_t tmp, bitmask; + + expr = stmt->payload.val; + + if (expr->op != OP_OR) + return false; + + value = expr->right; + if (value->etype != EXPR_VALUE) + return false; + + binop = expr->left; + if (binop->op != OP_AND) + return false; + + payload = binop->left; + if (payload->etype != EXPR_PAYLOAD) + return false; + + if (!payload_expr_cmp(stmt->payload.expr, payload)) + return false; + + mask = binop->right; + if (mask->etype != EXPR_VALUE) + return false; + + mpz_init(tmp); + mpz_set(tmp, mask->value); + + mpz_init_bitmask(bitmask, payload->len); + mpz_xor(bitmask, bitmask, mask->value); + mpz_xor(bitmask, bitmask, value->value); + mpz_set(mask->value, bitmask); + mpz_clear(bitmask); + + binop_postprocess(ctx, expr); + if (!payload_is_known(payload)) { + mpz_set(mask->value, tmp); + } else { + expr_free(stmt->payload.expr); + stmt->payload.expr = expr_get(payload); + stmt->payload.val = expr_get(expr->right); + expr_free(expr); + } + + mpz_clear(tmp); + + return true; +} + +static bool stmt_payload_binop_postprocess_ii(struct rule_pp_ctx *ctx) +{ + struct expr *expr, *payload, *value; + struct stmt *stmt = ctx->stmt; + mpz_t bitmask; + + expr = stmt->payload.val; + + value = expr->right; + if (value->etype != EXPR_VALUE) + return false; + + switch (expr->op) { + case OP_AND: /* IIa */ + payload = expr->left; + mpz_init_bitmask(bitmask, payload->len); + mpz_xor(bitmask, bitmask, value->value); + mpz_set(value->value, bitmask); + mpz_clear(bitmask); + break; + case OP_OR: /* IIb */ + break; + default: /* No idea */ + return false; + } + + stmt_payload_binop_pp(ctx, expr); + if (!payload_is_known(expr->left)) + return false; + + expr_free(stmt->payload.expr); + + switch (expr->op) { + case OP_AND: + /* Mask was used to match payload, i.e. + * user asked to set zero value. + */ + mpz_set_ui(value->value, 0); + break; + default: + break; + } + + stmt->payload.expr = expr_get(expr->left); + stmt->payload.val = expr_get(expr->right); + expr_free(expr); + return true; +} + /** * stmt_payload_binop_postprocess - decode payload set binop * @@ -2558,9 +2661,8 @@ static void stmt_payload_binop_pp(struct rule_pp_ctx *ctx, struct expr *binop) */ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx) { - struct expr *expr, *binop, *payload, *value, *mask; + struct expr *expr; struct stmt *stmt = ctx->stmt; - mpz_t bitmask; expr = stmt->payload.val; @@ -2568,93 +2670,13 @@ static void stmt_payload_binop_postprocess(struct rule_pp_ctx *ctx) return; switch (expr->left->etype) { - case EXPR_BINOP: {/* I? */ - mpz_t tmp; - - if (expr->op != OP_OR) - return; - - value = expr->right; - if (value->etype != EXPR_VALUE) + case EXPR_BINOP: /* I? */ + if (stmt_payload_binop_postprocess_i(ctx)) return; - - binop = expr->left; - if (binop->op != OP_AND) - return; - - payload = binop->left; - if (payload->etype != EXPR_PAYLOAD) - return; - - if (!payload_expr_cmp(stmt->payload.expr, payload)) - return; - - mask = binop->right; - if (mask->etype != EXPR_VALUE) - return; - - mpz_init(tmp); - mpz_set(tmp, mask->value); - - mpz_init_bitmask(bitmask, payload->len); - mpz_xor(bitmask, bitmask, mask->value); - mpz_xor(bitmask, bitmask, value->value); - mpz_set(mask->value, bitmask); - mpz_clear(bitmask); - - binop_postprocess(ctx, expr); - if (!payload_is_known(payload)) { - mpz_set(mask->value, tmp); - mpz_clear(tmp); - return; - } - - mpz_clear(tmp); - expr_free(stmt->payload.expr); - stmt->payload.expr = expr_get(payload); - stmt->payload.val = expr_get(expr->right); - expr_free(expr); break; - } case EXPR_PAYLOAD: /* II? */ - value = expr->right; - if (value->etype != EXPR_VALUE) - return; - - switch (expr->op) { - case OP_AND: /* IIa */ - payload = expr->left; - mpz_init_bitmask(bitmask, payload->len); - mpz_xor(bitmask, bitmask, value->value); - mpz_set(value->value, bitmask); - mpz_clear(bitmask); - break; - case OP_OR: /* IIb */ - break; - default: /* No idea */ + if (stmt_payload_binop_postprocess_ii(ctx)) return; - } - - stmt_payload_binop_pp(ctx, expr); - if (!payload_is_known(expr->left)) - return; - - expr_free(stmt->payload.expr); - - switch (expr->op) { - case OP_AND: - /* Mask was used to match payload, i.e. - * user asked to set zero value. - */ - mpz_set_ui(value->value, 0); - break; - default: - break; - } - - stmt->payload.expr = expr_get(expr->left); - stmt->payload.val = expr_get(expr->right); - expr_free(expr); break; default: /* No idea */ break; -- 2.25.1