netfilter: add hook dump feature
Subsystems (2)
Florian Westphal
maintainerv2 cover netfilter: add hook dump feature
Changes in v2:
Patch 1: init 'ret' to avoid unitialised value
Patch 4:
- include attribute that this is about nf_tables to allow later extension to x_tables if needed for some reason.
Enable dump of the registered netfilter hooks to userspace. This allows userspace to peek at the active hooks for each family/hook point.
Example:
$ nft list hook ip type 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]
}Implementation is done in nf_tables.
Alternative would be to add this as a separate/new nfnetlink family.
Let me know if thats the preferred route and I will respin. I did this in nf_tables because it allows re-use of the existing nft_hook_attributes and it seemed strange to add a new kernel module for this.
Florian Westphal (4):
netfilter: nf_tables: allow to dump all registered base hooks
netfilter: nf_tables: include function and module name in hook dumps
netfilter: annotate nf_tables base hook ops
netfilter: nf_tables: include table and chain name when dumping hooks
include/linux/netfilter.h | 12 +- include/uapi/linux/netfilter/nf_tables.h | 30 +++ net/netfilter/core.c | 6 + net/netfilter/nf_queue.c | 4 +- net/netfilter/nf_tables_api.c | 286 ++++++++++++++++++++++- 5 files changed, 334 insertions(+), 4 deletions(-)
Signature · 1 line
2.26.3
Florian Westphal
maintainerPATCH v2 1/4 netfilter: nf_tables: allow to dump all registered base hooks
This dumps the active hook pipeline to userspace.
Userspace needs to pass the address family and the hook point.
For the netdev family/ingress hook, a device name must be provided as well.
This allows 'nft' to display the priority of each hook.
Example:
family ip hook prerouting {
-0000000300
-0000000150
-0000000100
}... so this shows 3 active hooks with -300/-150/-100.
With manual work this is enough to figure out which hooks these are.
Followup patch will include the hook and module name (if any).
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nf_queue.c | 4 +- net/netfilter/nf_tables_api.c | 216 +++++++++++++++++++++++ 3 files changed, 219 insertions(+), 2 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -124,6 +124,7 @@ enum nf_tables_msg_types {NFT_MSG_NEWFLOWTABLE ,NFT_MSG_GETFLOWTABLE ,NFT_MSG_DELFLOWTABLE , +NFT_MSG_GETNFHOOKS , NFT_MSG_MAX, };
net/netfilter/nf_queue.c +2 -2
@@ -259,7 +259,7 @@ static unsigned int nf_iterate(struct sk_buff *skb,return NF_ACCEPT ; } -static struct nf_hook_entries *nf_hook_entries_head (const struct net *net,u8 pf,u8 hooknum) +static struct nf_hook_entries *nf_queue_hook_entries_head (const struct net *net,u8 pf,u8 hooknum) {switch (pf) {#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
@@ -292,7 +292,7 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) net = entry->state.net; pf = entry->state.pf; - hooks =nf_hook_entries_head (net, pf, entry->state.hook); + hooks =nf_queue_hook_entries_head (net, pf, entry->state.hook); i = entry->hook_index;if (WARN_ON_ONCE (!hooks || i >= hooks->num_hook_entries)) {
net/netfilter/nf_tables_api.c +216 -0
228 lines not on this page · raw message
Signature · 1 line
2.26.3
Florian Westphal
maintainerPATCH v2 2/4 netfilter: nf_tables: include function and module name in hook dumps
In reply to Florian WestphalIf KALLSYMS are available, include the hook function name and the module name that registered the hook.
This avoids need to manually annotate all existing hooks.
Example output:
family ip hook prerouting {
-0000000300 iptable_raw_hook [iptable_raw]
-0000000150 iptable_mangle_hook [iptable_mangle]
-0000000100 nf_nat_ipv4_pre_routing [nf_nat]
}--- include/linux/netfilter.h | 4 ++++ include/uapi/linux/netfilter/nf_tables.h | 4 ++++ net/netfilter/core.c | 6 ++++++ net/netfilter/nf_tables_api.c | 13 +++++++++++++ 4 files changed, 27 insertions(+)
include/linux/netfilter.h +4 -0
@@ -195,6 +195,10 @@ int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,void nf_hook_slow_list (struct list_head *head,struct nf_hook_state *state,const struct nf_hook_entries *e); + +bool nf_get_hook_info (const struct nf_hook_ops *ops, +char fn[KSYM_NAME_LEN ],char **module_name); +/** * nf_hook - call a netfilter hook *
include/uapi/linux/netfilter/nf_tables.h +4 -0
@@ -147,6 +147,8 @@ enum nft_list_attributes {* @NFTA_HOOK_PRIORITY: netfilter hook priority (NLA_U32) * @NFTA_HOOK_DEV: netdevice name (NLA_STRING) * @NFTA_HOOK_DEVS: list of netdevices (NLA_NESTED) +* @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING) +* @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING) */ enum nft_hook_attributes {NFTA_HOOK_UNSPEC ,
@@ -154,6 +156,8 @@ enum nft_hook_attributes {NFTA_HOOK_PRIORITY ,NFTA_HOOK_DEV ,NFTA_HOOK_DEVS , +NFTA_HOOK_FUNCTION_NAME , +NFTA_HOOK_MODULE_NAME , __NFTA_HOOK_MAX };#define NFTA_HOOK_MAX (__NFTA_HOOK_MAX - 1)
net/netfilter/core.c +6 -0
@@ -749,6 +749,12 @@ static struct pernet_operations netfilter_net_ops = { .exit = netfilter_net_exit, }; +bool nf_get_hook_info (const struct nf_hook_ops *ops,char fn[KSYM_NAME_LEN ],char **modname) +{ +return kallsyms_lookup ((unsigned long )ops->hook,NULL ,NULL , modname, fn); +} +EXPORT_SYMBOL_GPL (nf_get_hook_info); +int __init netfilter_init (void ) {int ret;
net/netfilter/nf_tables_api.c +13 -0
@@ -7983,6 +7983,7 @@ static int nf_tables_dump_one_hook(struct sk_buff *nlskb, {unsigned int portid =NETLINK_CB (nlskb).portid;struct net *net =sock_net (nlskb->sk); +char *module_name, fn[KSYM_NAME_LEN ];struct nlmsghdr *nlh;int ret = -EMSGSIZE ;
@@ -7991,6 +7992,18 @@ static int nf_tables_dump_one_hook(struct sk_buff *nlskb,if (!nlh) goto nla_put_failure; +if (nf_get_hook_info (ops, fn, &module_name)) { + ret =nla_put_string (nlskb,NFTA_HOOK_FUNCTION_NAME , fn); +if (ret) + goto nla_put_failure; + +if (module_name) { + ret =nla_put_string (nlskb,NFTA_HOOK_MODULE_NAME , module_name); +if (ret) + goto nla_put_failure; + } + } + ret =nla_put_be32 (nlskb,NFTA_HOOK_HOOKNUM ,htonl (ops->hooknum));if (ret) goto nla_put_failure;
Signature · 1 line
2.26.3
Florian Westphal
maintainerPATCH v2 3/4 netfilter: annotate nf_tables base hook ops
In reply to Florian WestphalThis will allow a followup patch to treat the 'ops->priv' pointer as nft_chain argument without having to first walk to table/chains for a match.
--- include/linux/netfilter.h | 8 +++++++- net/netfilter/nf_tables_api.c | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-)
include/linux/netfilter.h +7 -1
@@ -77,12 +77,18 @@ struct nf_hook_state {typedef unsigned int nf_hookfn (void *priv,struct sk_buff *skb,const struct nf_hook_state *state); +enum nf_hook_ops_type { +NF_HOOK_OP_UNDEFINED , +NF_HOOK_OP_NF_TABLES , +}; +struct nf_hook_ops {/* User fills in from here down. */ nf_hookfn *hook;struct net_device *dev;void *priv; -u_int8_t pf; +u8 pf; +enum nf_hook_ops_type hook_ops_type:8 ;unsigned int hooknum;/* Hooks are ordered in ascending priority. */ int priority;
net/netfilter/nf_tables_api.c +3 -1
@@ -2172,8 +2172,10 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, }nft_trans_chain_policy (trans) =NFT_CHAIN_POLICY_UNSET ; -if (nft_is_base_chain (chain)) +if (nft_is_base_chain (chain)) { + basechain->ops.hook_ops_type =NF_HOOK_OP_NF_TABLES ;nft_trans_chain_policy (trans) = policy; + } err =nft_chain_add (table, chain);if (err <0 ) {
Signature · 1 line
2.26.3
Florian Westphal
maintainerPATCH v2 4/4 netfilter: nf_tables: include table and chain name when dumping hooks
In reply to Florian WestphalFor ip(6)tables, the function names will show 'raw', 'mangle', and so on, but for nf_tables the interpreter name is identical for all base chains in the same family, so its not easy to line up the defined chains with the hook function name.
To make it easier to see how the ruleset lines up with the defined hooks, extend the hook dump to include the chain+table name.
Example list:
family ip hook input {
-0000000150 iptable_mangle_hook [iptable_mangle]
+0000000000 nft_do_chain_inet [nf_tables] # nft table filter chain input
[..] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- include/uapi/linux/netfilter/nf_tables.h | 25 +++++++++++ net/netfilter/nf_tables_api.c | 53 ++++++++++++++++++++++++ 2 files changed, 78 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +25 -0
@@ -149,6 +149,7 @@ enum nft_list_attributes {* @NFTA_HOOK_DEVS: list of netdevices (NLA_NESTED) * @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING) * @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING) +* @NFTA_HOOK_CHAIN_INFO: basechain hook metadata (NLA_NESTED) */ enum nft_hook_attributes {NFTA_HOOK_UNSPEC ,
@@ -158,10 +159,34 @@ enum nft_hook_attributes {NFTA_HOOK_DEVS ,NFTA_HOOK_FUNCTION_NAME ,NFTA_HOOK_MODULE_NAME , +NFTA_HOOK_CHAIN_INFO , __NFTA_HOOK_MAX };#define NFTA_HOOK_MAX (__NFTA_HOOK_MAX - 1) +/** +* enum nft_chain_info_attributes - chain description +* +* NFTA_CHAIN_INFO_DESC: chain and table name (enum nft_table_attributes) (NLA_NESTED) +* NFTA_CHAIN_INFO_TYPE: chain type (enum nf_chain_type) (NLA_U32) +*/ +enum nft_chain_info_attributes { +NFTA_CHAIN_INFO_UNSPEC , +NFTA_CHAIN_INFO_DESC , +NFTA_CHAIN_INFO_TYPE , + __NFTA_CHAIN_INFO_MAX, +}; +#define NFTA_CHAIN_INFO_MAX (__NFTA_CHAIN_INFO_MAX - 1) + +/** +* enum nft_chain_info_attributes - chain description +* +* @NF_CHAININFO_TYPE_NF_TABLES nf_tables base chain +*/ +enum nf_chaininfo_type { +NF_CHAININFO_TYPE_NF_TABLES =0x1 , +}; +/** * enum nft_table_flags - nf_tables table flags *
net/netfilter/nf_tables_api.c +53 -0
65 lines not on this page · raw message
Signature · 1 line
2.26.3