mnl: flowtable support for extended netlink error reporting
Pablo Neira Ayuso
maintainerPATCH 1/2 mnl: flowtable support for extended netlink error reporting
This patch extends existing flowtable support to improve error reporting:
# nft add flowtable inet x y '{ devices = { x } ; }'
Error: Could not process rule: No such file or directory
add flowtable inet x y { devices = { x } ; }
^
# nft delete flowtable inet x y '{ devices = { x } ; }'
Error: Could not process rule: No such file or directory
delete flowtable inet x y { devices = { x } ; }
^
Signed-off-by: Pablo Neira Ayuso <[email protected]>
---
v2: generalize nft_dev_array() to accomodate requirements for basechain support. src/mnl.c | 160 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 67 deletions(-)
src/mnl.c +93 -67
@@ -718,6 +718,69 @@ err:/* * Chain */ + +struct nft_dev { +const char *ifname; +struct location *location; +}; + +static struct nft_dev *nft_dev_array (const struct expr *dev_expr) +{ +struct nft_dev *dev_array; +unsigned int ifname_len; +char ifname[IFNAMSIZ ]; +int i =0 , len =1 ; +struct expr *expr; + +list_for_each_entry (expr, &dev_expr->expressions, list) + len++; + + dev_array =xmalloc (sizeof (struct nft_dev ) * len); + +list_for_each_entry (expr, &dev_expr->expressions, list) { + ifname_len =div_round_up (expr->len,BITS_PER_BYTE ); +memset (ifname,0 ,sizeof (ifname)); +mpz_export_data (ifname, expr->value,BYTEORDER_HOST_ENDIAN , + ifname_len); + dev_array[i].ifname =xstrdup (ifname); + dev_array[i].location = &expr->location; + i++; + } + + dev_array[i].ifname =NULL ; + +return dev_array; +} + +static void nft_dev_array_free (const struct nft_dev *dev_array) +{ +int i =0 ; + +while (dev_array[i].ifname !=NULL ) +xfree (dev_array[i++].ifname); + +xfree (dev_array); +} + + +static void mnl_nft_devs_build (struct nlmsghdr *nlh,struct cmd *cmd, +struct expr *dev_expr,int attr) +{ +const struct nft_dev *dev_array; +struct nlattr *nest_dev; +int i; + + dev_array =nft_dev_array (dev_expr); + nest_dev =mnl_attr_nest_start (nlh, attr); +for (i =0 ; dev_array[i].ifname !=NULL ; i++) { +cmd_add_loc (cmd, nlh->nlmsg_len, dev_array[i].location); +mnl_attr_put_strz (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); + } + +mnl_attr_nest_end (nlh, nest_dev); +nft_dev_array_free (dev_array); +} +int mnl_nft_chain_add (struct netlink_ctx *ctx,struct cmd *cmd,unsigned int flags) {
@@ -1907,49 +1970,12 @@ err:return NULL ; } -static const char **nft_flowtable_dev_array (struct cmd *cmd) -{ -unsigned int ifname_len; -const char **dev_array; -char ifname[IFNAMSIZ ]; -int i =0 , len =1 ; -struct expr *expr; - -list_for_each_entry (expr, &cmd->flowtable->dev_expr->expressions, list) - len++; - - dev_array =xmalloc (sizeof (char *) * len); - -list_for_each_entry (expr, &cmd->flowtable->dev_expr->expressions, list) { - ifname_len =div_round_up (expr->len,BITS_PER_BYTE ); -memset (ifname,0 ,sizeof (ifname)); -mpz_export_data (ifname, expr->value,BYTEORDER_HOST_ENDIAN , - ifname_len); - dev_array[i++] =xstrdup (ifname); - } - - dev_array[i] =NULL ; - -return dev_array; -} - -static void nft_flowtable_dev_array_free (const char **dev_array) -{ -int i =0 ; - -while (dev_array[i] !=NULL ) -xfree (dev_array[i++]); - -free (dev_array); -} -int mnl_nft_flowtable_add (struct netlink_ctx *ctx,struct cmd *cmd,unsigned int flags) {struct nftnl_flowtable *flo; -const char **dev_array;struct nlmsghdr *nlh; -int priority; +struct nlattr *nest; flo =nftnl_flowtable_alloc ();if (!flo)
@@ -1958,24 +1984,6 @@ int mnl_nft_flowtable_add(struct netlink_ctx *ctx, struct cmd *cmd,nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_FAMILY , cmd->handle.family); -if (cmd->flowtable->hook.name) { -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_HOOKNUM , - cmd->flowtable->hook.num); -mpz_export_data (&priority, cmd->flowtable->priority.expr->value, -BYTEORDER_HOST_ENDIAN ,sizeof (int )); -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_PRIO , priority); - }else { -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_HOOKNUM ,0 ); -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_PRIO ,0 ); - } - -if (cmd->flowtable->dev_expr) { - dev_array =nft_flowtable_dev_array (cmd); -nftnl_flowtable_set_data (flo,NFTNL_FLOWTABLE_DEVICES , - dev_array,0 ); -nft_flowtable_dev_array_free (dev_array); - } -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_FLAGS , cmd->flowtable->flags);
@@ -1991,6 +1999,25 @@ int mnl_nft_flowtable_add(struct netlink_ctx *ctx, struct cmd *cmd,mnl_attr_put_strz (nlh,NFTA_FLOWTABLE_NAME , cmd->handle.flowtable.name);nftnl_flowtable_nlmsg_build_payload (nlh, flo); + + nest =mnl_attr_nest_start (nlh,NFTA_FLOWTABLE_HOOK ); + +if (cmd->flowtable->hook.name) { +int priority; + +mnl_attr_put_u32 (nlh,NFTA_FLOWTABLE_HOOK_NUM , cmd->flowtable->hook.num); +mpz_export_data (&priority, cmd->flowtable->priority.expr->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +mnl_attr_put_u32 (nlh,NFTA_FLOWTABLE_HOOK_PRIORITY , priority); + } + +if (cmd->flowtable->dev_expr) { +mnl_nft_devs_build (nlh, cmd, cmd->flowtable->dev_expr, +NFTA_FLOWTABLE_HOOK_DEVS ); + } + +mnl_attr_nest_end (nlh, nest); +nftnl_flowtable_free (flo);mnl_nft_batch_continue (ctx->batch);
@@ -2002,8 +2029,8 @@ int mnl_nft_flowtable_del(struct netlink_ctx *ctx, struct cmd *cmd) {enum nf_tables_msg_types msg_type =NFT_MSG_DELFLOWTABLE ;struct nftnl_flowtable *flo; -const char **dev_array;struct nlmsghdr *nlh; +struct nlattr *nest; flo =nftnl_flowtable_alloc ();if (!flo)
@@ -2012,16 +2039,6 @@ int mnl_nft_flowtable_del(struct netlink_ctx *ctx, struct cmd *cmd)nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_FAMILY , cmd->handle.family); -if (cmd->flowtable && cmd->flowtable->dev_expr) { -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_HOOKNUM ,0 ); -nftnl_flowtable_set_u32 (flo,NFTNL_FLOWTABLE_PRIO ,0 ); - - dev_array =nft_flowtable_dev_array (cmd); -nftnl_flowtable_set_data (flo,NFTNL_FLOWTABLE_DEVICES , - dev_array,0 ); -nft_flowtable_dev_array_free (dev_array); - } -if (cmd->op ==CMD_DESTROY ) msg_type =NFT_MSG_DESTROYFLOWTABLE ;
@@ -2044,6 +2061,15 @@ int mnl_nft_flowtable_del(struct netlink_ctx *ctx, struct cmd *cmd) }nftnl_flowtable_nlmsg_build_payload (nlh, flo); + +if (cmd->op ==CMD_DELETE && + cmd->flowtable && cmd->flowtable->dev_expr) { + nest =mnl_attr_nest_start (nlh,NFTA_FLOWTABLE_HOOK ); +mnl_nft_devs_build (nlh, cmd, cmd->flowtable->dev_expr, +NFTA_FLOWTABLE_HOOK_DEVS ); +mnl_attr_nest_end (nlh, nest); + } +nftnl_flowtable_free (flo);mnl_nft_batch_continue (ctx->batch);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 src: allow for updating devices on existing netdev chain
This patch allows you to add/remove devices to an existing chain:
# cat ruleset.nft
table netdev x {
chain y {
type filter hook ingress devices = { eth0 } priority 0; policy accept;
}
}
# nft -f ruleset.nft
# nft add chain netdev x y '{ devices = { eth1 }; }'
# nft list ruleset
table netdev x {
chain y {
type filter hook ingress devices = { eth0, eth1 } priority 0; policy accept;
}
}
# nft delete chain netdev x y '{ devices = { eth0 }; }'
# nft list ruleset
table netdev x {
chain y {
type filter hook ingress devices = { eth1 } priority 0; policy accept;
}
}This feature allows for creating an empty netdev chain, with no devices. In such case, no packets are seen until a device is registered.
This patch includes extended netlink error reporting:
# nft add chain netdev x y '{ devices = { x } ; }'
Error: Could not process rule: No such file or directory
add chain netdev x y { devices = { x } ; }
^Signed-off-by Pablo Neira Ayuso <[email protected]>
---
v2: it includes extended netlink error reporting support.
src/evaluate.c | 12 +-- src/mnl.c | 90 ++++++++----------- src/monitor.c | 11 ++- src/parser_bison.y | 16 ++++ src/rule.c | 17 +++- .../testcases/chains/dumps/netdev_chain_0.nft | 5 ++ tests/shell/testcases/chains/netdev_chain_0 | 33 +++++++ 7 files changed, 121 insertions(+), 63 deletions(-) create mode 100644 tests/shell/testcases/chains/dumps/netdev_chain_0.nft create mode 100755 tests/shell/testcases/chains/netdev_chain_0
src/evaluate.c +7 -5
@@ -4964,15 +4964,17 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)return chain_error (ctx, chain,"invalid policy expression %s" ,expr_name (chain->policy)); } + } + +if (chain->dev_expr) { +if (!(chain->flags &CHAIN_F_BASECHAIN )) + chain->flags |=CHAIN_F_BASECHAIN ;if (chain->handle.family ==NFPROTO_NETDEV || (chain->handle.family ==NFPROTO_INET && chain->hook.num ==NF_INET_INGRESS )) { -if (!chain->dev_expr) -return __stmt_binary_error (ctx, &chain->loc,NULL , -"Missing `device' in this chain definition" ); - -if (!evaluate_device_expr (ctx, &chain->dev_expr)) +if (chain->dev_expr && + !evaluate_device_expr (ctx, &chain->dev_expr))return -1 ; }else if (chain->dev_expr) { return __stmt_binary_error(ctx, &chain->dev_expr->location, NULL,
src/mnl.c +38 -52
123 lines not on this page · raw message
src/monitor.c +7 -4
@@ -272,10 +272,13 @@ static int netlink_events_chain_cb(const struct nlmsghdr *nlh, int type,chain_print_plain (c, &monh->ctx->nft->output);break ;case NFT_MSG_DELCHAIN : -nft_mon_print (monh,"chain %s %s %s" , -family2str (c->handle.family), - c->handle.table.name, - c->handle.chain.name); +if (c->dev_array_len >0 ) +chain_print_plain (c, &monh->ctx->nft->output); +else +nft_mon_print (monh,"chain %s %s %s" , +family2str (c->handle.family), + c->handle.table.name, + c->handle.chain.name);break ; }nft_mon_print (monh,"\n" );
src/parser_bison.y +16 -0
@@ -1358,6 +1358,13 @@ delete_cmd : TABLE table_or_id_spec { $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_CHAIN, &$2, &@$, NULL); } + | CHAIN chain_spec chain_block_alloc + '{' chain_block '}' + { + $5->location = @5; + handle_merge(&$3->handle, &$2); + $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_CHAIN, &$2, &@$, $5); + } | RULE ruleid_spec { $$ = cmd_alloc(CMD_DELETE, CMD_OBJ_RULE, &$2, &@$, NULL);
@@ -2004,6 +2011,15 @@ chain_block : /* empty */ { $$ = $<chain>-1; } list_add_tail(&$2->list, &$1->rules); $$ = $1; } + | chain_block DEVICES '=' flowtable_expr stmt_separator + { + if ($$->dev_expr) { + list_splice_init(&$4->expressions, &$$->dev_expr->expressions); + expr_free($4); + break; + } + $$->dev_expr = $4; + } | chain_block comment_spec stmt_separator { if (already_set($1->comment, &@2, state)) {
src/rule.c +15 -2
@@ -1091,8 +1091,21 @@ void chain_print_plain(const struct chain *chain, struct output_ctx *octx)if (chain->flags &CHAIN_F_BASECHAIN ) {mpz_export_data (&policy, chain->policy->value,BYTEORDER_HOST_ENDIAN ,sizeof (int )); - nft_print(octx, " { type %s hook %s priority %s; policy %s; }", - chain->type.str, chain->hook.name, +nft_print (octx," { type %s hook %s " , + chain->type.str, chain->hook.name); + +if (chain->dev_array_len >0 ) { +int i; + +nft_print (octx,"devices = { " ); +for (i =0 ; i < chain->dev_array_len; i++) { +nft_print (octx,"%s" , chain->dev_array[i]); +if (i +1 != chain->dev_array_len) +nft_print (octx,", " ); + } +nft_print (octx," } " ); + } + nft_print(octx, "priority %s; policy %s; }", prio2str(octx, priobuf, sizeof(priobuf), chain->handle.family, chain->hook.num, chain->priority.expr),
tests/shell/testcases/chains/dumps/netdev_chain_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table netdev x { + chain y { + type filter hook ingress devices = { d0, d1 } priority filter; policy accept; + } +}
tests/shell/testcases/chains/netdev_chain_0 new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/bash + +ip link add d0 type dummy || { + echo "Skipping, no dummy interface available" + exit 0 +} +trap "ip link del d0" EXIT + +ip link add d1 type dummy || { + echo "Skipping, no dummy interface available" + exit 0 +} +trap "ip link del d1" EXIT + +ip link add d2 type dummy || { + echo "Skipping, no dummy interface available" + exit 0 +} +trap "ip link del d2" EXIT + +set -e + +RULESET="table netdev x { + chain y { + type filter hook ingress priority 0; policy accept; + } +}" + +$NFT -f - <<< "$RULESET" + +$NFT add chain netdev x y '{ devices = { d0 }; }' +$NFT add chain netdev x y '{ devices = { d1, d2, lo }; }' +$NFT delete chain netdev x y '{ devices = { lo }; }'
Signature · 1 line
2.30.2