We are about to add support for a new payload binop that needs to be post-processed, so move the contents of the two major cases (I and II) into separate functions to keep the function size reasonable. Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx> --- src/netlink_delinearize.c | 191 +++++++++++++++++++++----------------- 1 file changed, 108 insertions(+), 83 deletions(-) diff --git a/src/netlink_delinearize.c b/src/netlink_delinearize.c index 8f19594a1633..4036646d57ac 100644 --- a/src/netlink_delinearize.c +++ b/src/netlink_delinearize.c @@ -2862,6 +2862,110 @@ 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, &expr->left); + 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 * @@ -2906,9 +3010,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; @@ -2916,93 +3019,15 @@ 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) - 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)) + case EXPR_BINOP: /* I? */ + if (stmt_payload_binop_postprocess_i(ctx)) 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, &expr->left); - 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) + if (stmt_payload_binop_postprocess_ii(ctx)) 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 */ - 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.35.1