Skip to contentSkip to conversation

netfilter: add hook dump feature

PATCH Superseded Florian Westphal 3 participants 11 messages May 21, 2021 – May 23, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: add hook dump feature

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 |   7 +
 net/netfilter/core.c                     |   6 +
 net/netfilter/nf_queue.c                 |   4 +-
 net/netfilter/nf_tables_api.c            | 275 ++++++++++++++++++++++-
 5 files changed, 300 insertions(+), 4 deletions(-)
Signature · 1 line
2.26.3
author maintainer
#1 lore raw

PATCH 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
index 19715e2679d1..5810e41eff33 100644
@@ -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
index bbd1209694b8..224238b20825 100644
@@ -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
#2 lore raw

Re: [PATCH nf-next 1/4] netfilter: nf_tables: allow to dump all registered base hooks

I love your patch! Perhaps something to improve:

Hi Florian,

I love your patch! Perhaps something to improve:

[auto build test WARNING on nf-next/master]

url:    https://github.com/0day-ci/linux/commits/Florian-Westphal/netfilter-add-hook-dump-feature/20210522-225655
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
config: x86_64-randconfig-r026-20210522 (attached as .config)
compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project e84a9b9bb3051c35dea993cdad7b3d2575638f85)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/0d9a4c5b44ef1ad2d9ed5c4c8cea7eae29560706
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Florian-Westphal/netfilter-add-hook-dump-feature/20210522-225655 git checkout 0d9a4c5b44ef1ad2d9ed5c4c8cea7eae29560706
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> net/netfilter/nf_tables_api.c:7991:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
           if (!nlh)
               ^~~~
   net/netfilter/nf_tables_api.c:8006:9: note: uninitialized use occurs here
           return ret;
                  ^~~
   net/netfilter/nf_tables_api.c:7991:2: note: remove the 'if' if its condition is always false
           if (!nlh)
           ^~~~~~~~~
   net/netfilter/nf_tables_api.c:7987:9: note: initialize the variable 'ret' to silence this warning
           int ret;
                  ^
                   = 0

1 warning generated.

vim +7991 net/netfilter/nf_tables_api.c

7979
7980 static int nf_tables_dump_one_hook(struct sk_buff *nlskb,
7981 const struct nft_dump_hooks_data *ctx,
7982 const struct nf_hook_ops *ops)
7983 {
7984 unsigned int portid = NETLINK_CB(nlskb).portid;
7985 struct net *net = sock_net(nlskb->sk);
7986 struct nlmsghdr *nlh;
7987 int ret;
7988
7989 nlh = nfnl_msg_put(nlskb, portid, ctx->seq, NFT_MSG_GETNFHOOKS,
7990 NLM_F_MULTI, ops->pf, NFNETLINK_V0, nft_base_seq(net));

> 7991		if (!nlh)

7992 goto nla_put_failure;
7993
7994 ret = nla_put_be32(nlskb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum));
7995 if (ret)
7996 goto nla_put_failure;
7997
7998 ret = nla_put_be32(nlskb, NFTA_HOOK_PRIORITY, htonl(ops->priority));
7999 if (ret)
8000 goto nla_put_failure;
8001
8002 nlmsg_end(nlskb, nlh);
8003 return 0;
8004 nla_put_failure:
8005 nlmsg_trim(nlskb, nlh);
8006 return ret;
8007 }
8008

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#3 lore raw

PATCH 2/4 netfilter: nf_tables: include function and module name in hook dumps

In reply to Florian Westphal

If 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
index f0f3a8354c3c..63f77794f5ed 100644
@@ -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
index 5810e41eff33..ba6545a32e34 100644
@@ -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
index 63d032191e62..d14715c568c8 100644
@@ -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
index 2bfa80e93658..216f2921be0f 100644
@@ -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;
 
@@ -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
author maintainer
#4 lore raw

PATCH 3/4 netfilter: annotate nf_tables base hook ops

In reply to Florian Westphal

This 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
index 63f77794f5ed..6c327689ff82 100644
@@ -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
index 216f2921be0f..935f46db16bb 100644
@@ -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
author maintainer
#5 lore raw

PATCH 4/4 netfilter: nf_tables: include table and chain name when dumping hooks

In reply to Florian Westphal

For 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 |  2 ++
 net/netfilter/nf_tables_api.c            | 42 ++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index ba6545a32e34..4822a837250d 100644
@@ -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_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)
  */
 enum nft_hook_attributes {
 	NFTA_HOOK_UNSPEC,
@@ -158,6 +159,7 @@ enum nft_hook_attributes {
 	NFTA_HOOK_DEVS,
 	NFTA_HOOK_FUNCTION_NAME,
 	NFTA_HOOK_MODULE_NAME,
+	NFTA_HOOK_NFT_CHAIN_INFO,
 	__NFTA_HOOK_MAX
 };
 #define NFTA_HOOK_MAX		(__NFTA_HOOK_MAX - 1)
net/netfilter/nf_tables_api.c +42 -0

54 lines not on this page · raw message

Signature · 1 line
2.26.3
#6 lore raw

Re: [PATCH nf-next 4/4] netfilter: nf_tables: include table and chain name when dumping hooks

Quoted 25 lines · Florian Westphal

For 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 |  2 ++
 net/netfilter/nf_tables_api.c            | 42 ++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +0 -0
index ba6545a32e34..4822a837250d 100644

include/uapi/linux/netfilter/nf_tables.h enum nft_list_attributes { · line 152

Quoted 4 lines
include/uapi/linux/netfilter/nf_tables.h +1 -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_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)

Probably NFTA_HOOK_CHAIN_INFO ?

author maintainer
#7 lore raw

Re: [PATCH nf-next 4/4] netfilter: nf_tables: include table and chain name when dumping hooks

Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Florian Westphal

* @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING)
* @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING)
+ * @NFTA_HOOK_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)

Probably NFTA_HOOK_CHAIN_INFO ?

I added _NFT_ to avoid ambiguity in case this would be extended to add xt-legacy chain info. I can drop the _NFT_, let me know.

#8 lore raw

Re: [PATCH nf-next 4/4] netfilter: nf_tables: include table and chain name when dumping hooks

Quoted 10 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Florian Westphal

* @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING)
* @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING)
+ * @NFTA_HOOK_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)

Probably NFTA_HOOK_CHAIN_INFO ?

I added _NFT_ to avoid ambiguity in case this would be extended to add xt-legacy chain info. I can drop the _NFT_, let me know.

It's a NLA_NESTED, you might add a _TYPE field inside the nest to describe what type of chain info is stored there, maybe?

author maintainer
#9 lore raw

Re: [PATCH nf-next 4/4] netfilter: nf_tables: include table and chain name when dumping hooks

Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Pablo Neira Ayuso
Quoted 3 lines · Florian Westphal

* @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING)
* @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING)
+ * @NFTA_HOOK_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Probably NFTA_HOOK_CHAIN_INFO ?

I added _NFT_ to avoid ambiguity in case this would be extended to add xt-legacy chain info. I can drop the _NFT_, let me know.

It's a NLA_NESTED, you might add a _TYPE field inside the nest to describe what type of chain info is stored there, maybe?

It uses enum nft_chain_attributes, it somehow feels wrong to add a 'type' field for that.

I could add a new enum if you prefer.

At this point I don't think adding xt specific info is useful because the chain function name already tell if its mangle, raw etc.

#10 lore raw

Re: [PATCH nf-next 4/4] netfilter: nf_tables: include table and chain name when dumping hooks

Quoted 10 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 3 lines · Florian Westphal

* @NFTA_HOOK_FUNCTION_NAME: hook function name (NLA_STRING)
* @NFTA_HOOK_MODULE_NAME: kernel module that registered this hook (NLA_STRING)
+ * @NFTA_HOOK_NFT_CHAIN_INFO: nft chain and table name (NLA_NESTED)

Probably NFTA_HOOK_CHAIN_INFO ?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I added _NFT_ to avoid ambiguity in case this would be extended to add xt-legacy chain info. I can drop the _NFT_, let me know.

It's a NLA_NESTED, you might add a _TYPE field inside the nest to describe what type of chain info is stored there, maybe?

It uses enum nft_chain_attributes, it somehow feels wrong to add a 'type' field for that.

Agreed. Probably another nest level.

NFTA_HOOK_CHAIN_INFO
CHAIN_INFO_DESC
nft_chain_attributes
CHAIN_INFO_TYPE

Quoted 4 lines

I could add a new enum if you prefer.

At this point I don't think adding xt specific info is useful because the chain function name already tell if its mangle, raw etc.

I'd prefer to not expose internal kernel functions names, but I understand this approach is simple.