src: support for implicit chain bindings
Pablo Neira Ayuso
maintainerPATCH 1/1 src: support for implicit chain bindings
This patch adds allows you to define implicit chain bindings, e.g.
table inet x {
chain y {
type filter hook input priority 0;
tcp dport 22 jump {
ip saddr { 127.0.0.0/8, 172.23.0.0/16, 192.168.13.0/24 } accept
ip6 saddr ::1/128 accept;
}
}}
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/expression.h | 1 + include/linux/netfilter/nf_tables.h | 2 ++ include/netlink.h | 1 + include/parser.h | 2 +- include/rule.h | 2 ++ include/statement.h | 10 +++++++ src/evaluate.c | 29 ++++++++++++++++++++ src/mnl.c | 14 ++++++++-- src/netlink.c | 42 ++++++++++++++++++----------- src/netlink_linearize.c | 14 ++++++++-- src/parser_bison.y | 13 +++++++++ src/rule.c | 9 +++++++ src/statement.c | 20 ++++++++++++++ 13 files changed, 138 insertions(+), 21 deletions(-)
include/expression.h +1 -0
@@ -249,6 +249,7 @@ struct expr {/* EXPR_VERDICT */ int verdict;struct expr *chain; +uint32_t chain_id; };struct {/* EXPR_VALUE */
include/linux/netfilter/nf_tables.h +2 -0
@@ -209,6 +209,7 @@ enum nft_chain_attributes {NFTA_CHAIN_COUNTERS ,NFTA_CHAIN_PAD ,NFTA_CHAIN_FLAGS , +NFTA_CHAIN_ID , __NFTA_CHAIN_MAX };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)
@@ -238,6 +239,7 @@ enum nft_rule_attributes {NFTA_RULE_PAD ,NFTA_RULE_ID ,NFTA_RULE_POSITION_ID , +NFTA_RULE_CHAIN_ID , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1)
include/netlink.h +1 -0
@@ -83,6 +83,7 @@ struct nft_data_linearize {uint32_t len;uint32_t value[4 ];char chain[NFT_CHAIN_MAXNAMELEN ]; +uint32_t chain_id;int verdict; };
include/parser.h +1 -1
@@ -11,7 +11,7 @@#define YYLTYPE_IS_TRIVIAL 0#define YYENABLE_NLS 0 -#define SCOPE_NEST_MAX 3 +#define SCOPE_NEST_MAX 4struct parser_state {struct input_descriptor *indesc;
include/rule.h +2 -0
@@ -79,6 +79,7 @@ struct handle {struct position_spec position;struct position_spec index;uint32_t set_id; +uint32_t chain_id;uint32_t rule_id;uint32_t position_id; };
@@ -176,6 +177,7 @@ extern struct table *table_lookup_fuzzy(const struct handle *h,enum chain_flags {CHAIN_F_BASECHAIN =0x1 ,CHAIN_F_HW_OFFLOAD =0x2 , +CHAIN_F_BINDING =0x4 , };/**
include/statement.h +10 -0
@@ -11,6 +11,13 @@ extern struct stmt *expr_stmt_alloc(const struct location *loc,extern struct stmt *verdict_stmt_alloc (const struct location *loc,struct expr *expr); +struct chain_stmt { +struct chain *chain; +struct expr *expr; +}; + +struct stmt *chain_stmt_alloc (const struct location *loc,struct chain *chain); +struct flow_stmt {const char *table_name; };
@@ -287,6 +294,7 @@ extern struct stmt *xt_stmt_alloc(const struct location *loc);* @STMT_CONNLIMIT: connection limit statement * @STMT_MAP: map statement * @STMT_SYNPROXY: synproxy statement +* @STMT_CHAIN: chain statement */ enum stmt_types {STMT_INVALID ,
@@ -315,6 +323,7 @@ enum stmt_types {STMT_CONNLIMIT ,STMT_MAP ,STMT_SYNPROXY , + STMT_CHAIN, };/**
@@ -380,6 +389,7 @@ struct stmt {struct flow_stmt flow;struct map_stmt map;struct synproxy_stmt synproxy; +struct chain_stmt chain; }; };
src/evaluate.c +29 -0
@@ -3094,6 +3094,33 @@ static int stmt_evaluate_synproxy(struct eval_ctx *ctx, struct stmt *stmt)return 0 ; } +static int stmt_evaluate_chain (struct eval_ctx *ctx,struct stmt *stmt) +{ +struct chain *chain = stmt->chain.chain; +struct cmd *cmd; +struct handle h; + +handle_merge (&chain->handle, &ctx->cmd->handle); + chain->handle.chain.location = ctx->cmd->handle.chain.location; + chain->handle.chain.name =xstrdup ("__chain%d" ); + chain->flags |=CHAIN_F_BINDING ; + +memset (&h,0 ,sizeof (h)); +handle_merge (&h, &chain->handle); + h.chain.location = stmt->location; + +if (ctx->table !=NULL ) { +list_add_tail (&chain->list, &ctx->table->chains); + }else { + cmd =cmd_alloc (CMD_ADD ,CMD_OBJ_CHAIN , &h, &stmt->location, + chain); + cmd->location = stmt->location; +list_add_tail (&cmd->list, &ctx->cmd->list); + } + +return 0 ; +} +static int stmt_evaluate_dup (struct eval_ctx *ctx,struct stmt *stmt) {int err;
@@ -3440,6 +3467,8 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)return stmt_evaluate_map (ctx, stmt);case STMT_SYNPROXY :return stmt_evaluate_synproxy (ctx, stmt); +case STMT_CHAIN : +return stmt_evaluate_chain (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/mnl.c +12 -2
@@ -466,7 +466,10 @@ int mnl_nft_rule_add(struct netlink_ctx *ctx, struct cmd *cmd,cmd_add_loc (cmd, nlh->nlmsg_len, &h->table.location);mnl_attr_put_strz (nlh,NFTA_RULE_TABLE , h->table.name);cmd_add_loc (cmd, nlh->nlmsg_len, &h->chain.location); -mnl_attr_put_strz (nlh,NFTA_RULE_CHAIN , h->chain.name); +if (h->chain_id) +mnl_attr_put_u32 (nlh,NFTA_RULE_CHAIN_ID ,htonl (h->chain_id)); +else +mnl_attr_put_strz (nlh,NFTA_RULE_CHAIN , h->chain.name);nftnl_rule_nlmsg_build_payload (nlh, nlr);nftnl_rule_free (nlr);
@@ -679,7 +682,14 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd,cmd_add_loc (cmd, nlh->nlmsg_len, &cmd->handle.table.location);mnl_attr_put_strz (nlh,NFTA_CHAIN_TABLE , cmd->handle.table.name);cmd_add_loc (cmd, nlh->nlmsg_len, &cmd->handle.chain.location); -mnl_attr_put_strz (nlh,NFTA_CHAIN_NAME , cmd->handle.chain.name); + +if (!cmd->chain || !(cmd->chain->flags &CHAIN_F_BINDING )) { +mnl_attr_put_strz (nlh,NFTA_CHAIN_NAME , cmd->handle.chain.name); + }else { +mnl_attr_put_u32 (nlh,NFTA_CHAIN_ID ,htonl (cmd->handle.chain_id)); +if (cmd->chain->flags) +nftnl_chain_set_u32 (nlc,NFTNL_CHAIN_FLAGS , cmd->chain->flags); + }if (cmd->chain && cmd->chain->policy) { mpz_export_data(&policy, cmd->chain->policy->value,
src/netlink.c +26 -16
57 lines not on this page · raw message
src/netlink_linearize.c +12 -2
@@ -712,10 +712,12 @@ static void netlink_gen_immediate(struct netlink_linearize_ctx *ctx,nftnl_expr_set (nle,NFTNL_EXPR_IMM_DATA , nld.value, nld.len);break ;case EXPR_VERDICT : -if ((expr->chain !=NULL ) && - !nftnl_expr_is_set (nle,NFTNL_EXPR_IMM_CHAIN )) { +if (expr->chain !=NULL ) {nftnl_expr_set_str (nle,NFTNL_EXPR_IMM_CHAIN , nld.chain); + }else if (expr->chain_id) { +nftnl_expr_set_u32 (nle,NFTNL_EXPR_IMM_CHAIN_ID , + nld.chain_id); }nftnl_expr_set_u32 (nle,NFTNL_EXPR_IMM_VERDICT , nld.verdict);break ;
@@ -1442,6 +1444,12 @@ static void netlink_gen_meter_stmt(struct netlink_linearize_ctx *ctx,nftnl_rule_add_expr (ctx->nlr, nle); } +static void netlink_gen_chain_stmt (struct netlink_linearize_ctx *ctx, +const struct stmt *stmt) +{ +return netlink_gen_expr (ctx, stmt->chain.expr,NFT_REG_VERDICT ); +} +static void netlink_gen_stmt (struct netlink_linearize_ctx *ctx,const struct stmt *stmt) {
@@ -1495,6 +1503,8 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,return netlink_gen_objref_stmt (ctx, stmt);case STMT_MAP :return netlink_gen_map_stmt (ctx, stmt); +case STMT_CHAIN : +return netlink_gen_chain_stmt (ctx, stmt);default :BUG ("unknown statement type %s\n" , stmt->ops->name); }
src/parser_bison.y +13 -0
@@ -642,6 +642,8 @@ int nft_lex(void *, void *, void *); %destructor { stmt_free($$); } tproxy_stmt %type <stmt> synproxy_stmt synproxy_stmt_alloc %destructor { stmt_free($$); } synproxy_stmt synproxy_stmt_alloc +%type <stmt> chain_stmt +%destructor { stmt_free($$); } chain_stmt %type <stmt> queue_stmt queue_stmt_alloc
@@ -2527,6 +2529,17 @@ stmt : verdict_stmt | set_stmt | map_stmt | synproxy_stmt + | chain_stmt + ; + +chain_stmt : JUMP chain_block_alloc '{' chain_block '}' + { + struct handle h = {}; + $4->location = @4; + handle_merge(&h, &$2->handle); + close_scope(state); + $$ = chain_stmt_alloc(&@$, $4); + } ; verdict_stmt : verdict_expr
src/rule.c +9 -0
@@ -858,12 +858,16 @@ const char *chain_hookname_lookup(const char *name)return NULL ; } +/* internal ID to uniquely identify a set in the batch */ +static uint32_t chain_id; +struct chain *chain_alloc (const char *name) {struct chain *chain; chain =xzalloc (sizeof (*chain)); chain->refcnt =1 ; + chain->handle.chain_id = ++chain_id;init_list_head (&chain->rules);init_list_head (&chain->scope.symbols);if (name !=NULL )
@@ -1437,6 +1441,7 @@ void nft_cmd_expand(struct cmd *cmd)list_for_each_entry (chain, &table->chains, list) {memset (&h,0 ,sizeof (h));handle_merge (&h, &chain->handle); + h.chain_id = chain->handle.chain_id; new =cmd_alloc (CMD_ADD ,CMD_OBJ_CHAIN , &h, &chain->location,chain_get (chain));list_add_tail (&new->list, &new_cmds);
@@ -1469,6 +1474,10 @@ void nft_cmd_expand(struct cmd *cmd)list_for_each_entry (rule, &chain->rules, list) {memset (&h,0 ,sizeof (h));handle_merge (&h, &rule->handle); +if (chain->flags &CHAIN_F_BINDING ) { + rule->handle.chain_id = + chain->handle.chain_id; + } new =cmd_alloc (CMD_ADD ,CMD_OBJ_RULE , &h, &rule->location,rule_get (rule));
src/statement.c +20 -0
@@ -15,6 +15,7 @@#include <inttypes.h> #include <string.h> #include <syslog.h> +#include <rule.h> #include <arpa/inet.h> #include <linux/netfilter.h>
@@ -111,6 +112,25 @@ struct stmt *verdict_stmt_alloc(const struct location *loc, struct expr *expr)return stmt; } +static const struct stmt_ops chain_stmt_ops = { + .type =STMT_CHAIN , + .name ="chain" , + .print = expr_stmt_print, + .destroy = expr_stmt_destroy, +}; + +struct stmt *chain_stmt_alloc (const struct location *loc,struct chain *chain) +{ +struct stmt *stmt; + + stmt =stmt_alloc (loc, &chain_stmt_ops); + stmt->chain.chain = chain; + stmt->chain.expr =verdict_expr_alloc (loc,NFT_JUMP ,NULL ); + stmt->chain.expr->chain_id = chain->handle.chain_id; + +return stmt; +} +static void meter_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {unsigned int flags = octx->flags;
Signature · 1 line
2.20.1