evaluate: check if table and chain exists when adding rules
Pablo Neira Ayuso
maintainerPATCH 1/2 evaluate: check if table and chain exists when adding rules
Assuming a table 'test' that contains a chain 'test':
# nft add rule test1 test2 counter <cmdline>:1:1-28: Error: Could not process rule: Table 'test1' does not exist add rule test1 test2 counter ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # nft add rule test test2 counter <cmdline>:1:1-27: Error: Could not process rule: Chain 'test2' does not exist add rule test test2 counter ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/evaluate.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
src/evaluate.c +11 -0
@@ -2050,6 +2050,8 @@ static int table_evaluate(struct eval_ctx *ctx, struct table *table)static int cmd_evaluate_add (struct eval_ctx *ctx,struct cmd *cmd) { +struct table *table; +switch (cmd->obj) {case CMD_OBJ_SETELEM :return setelem_evaluate (ctx, &cmd->expr);
@@ -2058,6 +2060,15 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)return set_evaluate (ctx, cmd->set);case CMD_OBJ_RULE :handle_merge (&cmd->rule->handle, &cmd->handle); + table =table_lookup_global (ctx); +if (table ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + ctx->cmd->handle.table); + +if (chain_lookup (table, &ctx->cmd->handle) ==NULL ) +return cmd_error (ctx,"Could not process rule: Chain '%s' does not exist" , + ctx->cmd->handle.chain); +return rule_evaluate (ctx, cmd->rule);case CMD_OBJ_CHAIN :return chain_evaluate (ctx, cmd->chain);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/2 netlink_linearize: factor out prefix generation
Add a new netlink_gen_prefix() function that encapsulates the prefix generation.
Signed-off-by Pablo Neira Ayuso <[email protected]>
---
This patch, I should have sent it before the one to add wildcard string.
src/netlink_linearize.c | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-)
src/netlink_linearize.c +31 -22
@@ -290,13 +290,40 @@ static void payload_shift_value(const struct expr *left, struct expr *right)mpz_lshift_ui (right->value, left->payload.offset %BITS_PER_BYTE ); } +static struct expr *netlink_gen_prefix (struct netlink_linearize_ctx *ctx, +const struct expr *expr, +enum nft_registers sreg) +{ +struct nft_data_linearize nld, zero = {}; +struct nftnl_expr *nle; +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); + + zero.len = nld.len; + + nle =alloc_nft_expr ("bitwise" ); +netlink_put_register (nle,NFTNL_EXPR_BITWISE_SREG , sreg); +netlink_put_register (nle,NFTNL_EXPR_BITWISE_DREG , sreg); +nftnl_expr_set_u32 (nle,NFTNL_EXPR_BITWISE_LEN , nld.len); +nftnl_expr_set (nle,NFTNL_EXPR_BITWISE_MASK , &nld.value, nld.len); +nftnl_expr_set (nle,NFTNL_EXPR_BITWISE_XOR , &zero.value, zero.len); +nftnl_rule_add_expr (ctx->nlr, nle); + +return expr->right->prefix; +} +static void netlink_gen_cmp (struct netlink_linearize_ctx *ctx,const struct expr *expr,enum nft_registers dreg) { +struct nft_data_linearize nld;struct nftnl_expr *nle;enum nft_registers sreg; -struct nft_data_linearize nld, zero = {};struct expr *right;assert (dreg ==NFT_REG_VERDICT );
@@ -308,30 +335,12 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,netlink_gen_expr (ctx, expr->left, sreg);switch (expr->right->ops->type) { -case EXPR_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); - - zero.len = nld.len; - - nle =alloc_nft_expr ("bitwise" ); -netlink_put_register (nle,NFTNL_EXPR_BITWISE_SREG , sreg); -netlink_put_register (nle,NFTNL_EXPR_BITWISE_DREG , sreg); -nftnl_expr_set_u32 (nle,NFTNL_EXPR_BITWISE_LEN , nld.len); -nftnl_expr_set (nle,NFTNL_EXPR_BITWISE_MASK , &nld.value, nld.len); -nftnl_expr_set (nle,NFTNL_EXPR_BITWISE_XOR , &zero.value, zero.len); -nftnl_rule_add_expr (ctx->nlr, nle); - - right = expr->right->prefix; +case EXPR_PREFIX : + right =netlink_gen_prefix (ctx, expr, sreg);break ; - }default : right = expr->right; +break ; } nle =alloc_nft_expr ("cmp" );
Signature · 1 line
2.1.4