scanner: add list cmd parser scope
Florian Westphal
maintainerPATCH v2 1/3 scanner: add list cmd parser scope
Followup patch will add new 'hooks' keyword for
nft list hooks
Add a scope for list to avoid exposure of the new keyword in nft rulesets.
Signed-off-by Florian Westphal <[email protected]>
---
No changes.
include/parser.h | 1 + src/parser_bison.y | 3 ++- src/scanner.l | 15 +++++++++------ 3 files changed, 12 insertions(+), 7 deletions(-)
include/parser.h +1 -0
@@ -41,6 +41,7 @@ enum startcond_type {PARSER_SC_SCTP ,PARSER_SC_SECMARK ,PARSER_SC_VLAN , +PARSER_SC_CMD_LIST ,PARSER_SC_EXPR_FIB ,PARSER_SC_EXPR_HASH , PARSER_SC_EXPR_IPSEC,
src/parser_bison.y +2 -1
@@ -913,6 +913,7 @@ close_scope_ip : { scanner_pop_start_cond(nft->scanner, PARSER_SC_IP); }; close_scope_ip6 : { scanner_pop_start_cond(nft->scanner, PARSER_SC_IP6); }; close_scope_vlan : { scanner_pop_start_cond(nft->scanner, PARSER_SC_VLAN); }; close_scope_ipsec : { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_IPSEC); }; +close_scope_list : { scanner_pop_start_cond(nft->scanner, PARSER_SC_CMD_LIST); }; close_scope_limit : { scanner_pop_start_cond(nft->scanner, PARSER_SC_LIMIT); }; close_scope_numgen : { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_NUMGEN); }; close_scope_quota : { scanner_pop_start_cond(nft->scanner, PARSER_SC_QUOTA); };
@@ -1004,7 +1005,7 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } | INSERT insert_cmd { $$ = $2; } | DELETE delete_cmd { $$ = $2; } | GET get_cmd { $$ = $2; } - | LIST list_cmd { $$ = $2; } + | LIST list_cmd close_scope_list { $$ = $2; } | RESET reset_cmd { $$ = $2; } | FLUSH flush_cmd { $$ = $2; } | RENAME rename_cmd { $$ = $2; }
src/scanner.l +9 -6
@@ -207,6 +207,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) %s SCANSTATE_SCTP %s SCANSTATE_SECMARK %s SCANSTATE_VLAN +%s SCANSTATE_CMD_LIST %s SCANSTATE_EXPR_FIB %s SCANSTATE_EXPR_HASH %s SCANSTATE_EXPR_IPSEC
@@ -317,7 +318,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "insert" { return INSERT; } "delete" { return DELETE; } "get" { return GET; } -"list" { return LIST; } +"list" { scanner_push_start_cond(yyscanner, SCANSTATE_CMD_LIST); return LIST; } "reset" { return RESET; } "flush" { return FLUSH; } "rename" { return RENAME; }
@@ -346,9 +347,14 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "flow" { return FLOW; } "offload" { return OFFLOAD; } "meter" { return METER; } -"meters" { return METERS; } -"flowtables" { return FLOWTABLES; } +<SCANSTATE_CMD_LIST>{ + "meters" { return METERS; } + "flowtables" { return FLOWTABLES; } + "limits" { return LIMITS; } + "secmarks" { return SECMARKS; } + "synproxys" { return SYNPROXYS; } +} "counter" { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; } "name" { return NAME; }
@@ -357,8 +363,6 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "counters" { return COUNTERS; } "quotas" { return QUOTAS; } -"limits" { return LIMITS; } -"synproxys" { return SYNPROXYS; } "log" { scanner_push_start_cond(yyscanner, SCANSTATE_STMT_LOG); return LOG; } "prefix" { return PREFIX; }
@@ -687,7 +691,6 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) } "secmark" { scanner_push_start_cond(yyscanner, SCANSTATE_SECMARK); return SECMARK; } -"secmarks" { return SECMARKS; } {addrstring} { yylval->string = xstrdup(yytext);
Signature · 1 line
2.31.1
Florian Westphal
maintainerPATCH v2 2/3 src: add support for base hook dumping
Example output:
$ nft list hook ip input
family ip hook input {
+0000000000 nft_do_chain_inet [nf_tables] # nft table ip filter chain input
+0000000010 nft_do_chain_inet [nf_tables] # nft table ip firewalld chain filter_INPUT
+0000000100 nf_nat_ipv4_local_in [nf_nat]
+2147483647 ipv4_confirm [nf_conntrack]
}$ nft list hooks netdev type ingress device lo
family netdev hook ingress device lo {
+0000000000 nft_do_chain_netdev [nf_tables]
}$ nft list hooks inet
family ip hook prerouting {
-0000000400 ipv4_conntrack_defrag [nf_defrag_ipv4]
-0000000300 iptable_raw_hook [iptable_raw]
-0000000290 nft_do_chain_inet [nf_tables] # nft table ip firewalld chain raw_PREROUTING
-0000000200 ipv4_conntrack_in [nf_conntrack]
-0000000140 nft_do_chain_inet [nf_tables] # nft table ip firewalld chain mangle_PREROUTING
-0000000100 nf_nat_ipv4_pre_routing [nf_nat]
}
...'nft list hooks' will display everyting except the netdev family via successive dump request for all family:hook combinations.
Signed-off-by Florian Westphal <[email protected]>
---
This reworked version makes use of the new nfnetlink_hook subsystem.
include/linux/netfilter/nfnetlink.h | 3 +- include/linux/netfilter/nfnetlink_hook.h | 54 ++++ include/mnl.h | 3 + include/rule.h | 1 + src/evaluate.c | 10 + src/mnl.c | 328 ++++++++++++++++++++++- src/parser_bison.y | 48 +++- src/rule.c | 13 + src/scanner.l | 1 + 9 files changed, 457 insertions(+), 4 deletions(-) create mode 100644 include/linux/netfilter/nfnetlink_hook.h
include/linux/netfilter/nfnetlink.h +2 -1
@@ -59,7 +59,8 @@ struct nfgenmsg {#define NFNL_SUBSYS_CTHELPER 9#define NFNL_SUBSYS_NFTABLES 10#define NFNL_SUBSYS_NFT_COMPAT 11 -#define NFNL_SUBSYS_COUNT 12 +#define NFNL_SUBSYS_HOOK 12 +#define NFNL_SUBSYS_COUNT 13/* Reserved control nfnetlink messages */ #define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE
include/linux/netfilter/nfnetlink_hook.h new file +54 -0
54 lines not on this page · raw message
include/mnl.h +3 -0
@@ -82,6 +82,9 @@ int mnl_nft_flowtable_add(struct netlink_ctx *ctx, struct cmd *cmd, unsigned int flags);int mnl_nft_flowtable_del (struct netlink_ctx *ctx,struct cmd *cmd); +int mnl_nft_dump_nf_hooks (struct netlink_ctx *ctx,int family,int hook, +const char *devname); + int mnl_nft_event_listener(struct mnl_socket *nf_sock, unsigned int debug_mask, struct output_ctx *octx, int (*cb)(const struct nlmsghdr *nlh, void *data),
include/rule.h +1 -0
@@ -644,6 +644,7 @@ enum cmd_obj {CMD_OBJ_CT_EXPECT ,CMD_OBJ_SYNPROXY ,CMD_OBJ_SYNPROXYS , + CMD_OBJ_HOOKS, }; struct markup {
src/evaluate.c +10 -0
@@ -4702,6 +4702,16 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)case CMD_OBJ_METERS :case CMD_OBJ_MAPS :return 0 ; +case CMD_OBJ_HOOKS : +if (cmd->handle.chain.name) { +int hooknum =str2hooknum (cmd->handle.family, cmd->handle.chain.name); + +if (hooknum ==NF_INET_NUMHOOKS ) +return chain_not_found (ctx); + + cmd->handle.chain_id = hooknum; + } +return 0 ;default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/mnl.c +327 -1
349 lines not on this page · raw message
src/parser_bison.y +46 -2
69 lines not on this page · raw message
src/rule.c +13 -0
@@ -2364,6 +2364,17 @@ static int do_list_set(struct netlink_ctx *ctx, struct cmd *cmd,return 0 ; } +static int do_list_hooks (struct netlink_ctx *ctx,struct cmd *cmd) +{ +const char *devname = cmd->handle.obj.name; +int hooknum =-1 ; + +if (cmd->handle.chain.name) + hooknum = cmd->handle.chain_id; + +return mnl_nft_dump_nf_hooks (ctx, cmd->handle.family, hooknum, devname); +} +static int do_command_list (struct netlink_ctx *ctx,struct cmd *cmd) {struct table *table =NULL ;
@@ -2424,6 +2435,8 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)return do_list_flowtable (ctx, cmd, table);case CMD_OBJ_FLOWTABLES :return do_list_flowtables (ctx, cmd); +case CMD_OBJ_HOOKS : +return do_list_hooks (ctx, cmd);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/scanner.l +1 -0
@@ -354,6 +354,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "limits" { return LIMITS; } "secmarks" { return SECMARKS; } "synproxys" { return SYNPROXYS; } + "hooks" { return HOOKS; } } "counter" { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
Signature · 1 line
2.31.1
Florian Westphal
maintainerPATCH v2 3/3 doc: add LISTING section
In reply to Florian Westphalmention various 'nft list' options, such as secmarks, flow tables, and so on.
Signed-off-by Florian Westphal <[email protected]>
---
No changes.
doc/nft.txt | 11 +++++++++++ 1 file changed, 11 insertions(+)
doc/nft.txt +11 -0
@@ -683,6 +683,16 @@ and subtraction can be used to set relative priority, e.g. filter + 5 equals to *delete*:: Delete the specified flowtable. *list*:: List all flowtables. +LISTING +------- +[verse] +*list { secmarks | synproxys | flow tables | meters | hooks }* ['family'] +*list { secmarks | synproxys | flow tables | meters | hooks } table* ['family'] 'table' +*list ct { timeout | expectation | helper | helpers } table* ['family'] 'table' + +Inspect configured objects. +*list hooks* shows the full hook pipeline, including those registered by +kernel modules, such as nf_conntrack. STATEFUL OBJECTS ----------------
@@ -691,6 +701,7 @@ STATEFUL OBJECTS *delete* 'type' ['family'] 'table' *handle* 'handle' *list counters* ['family'] *list quotas* ['family'] +*list limits* ['family'] Stateful objects are attached to tables and are identified by a unique name. They group stateful information from rules, to reference them in rules the
Signature · 1 line
2.31.1