1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2021 Red Hat GmbH
5 * Author: Florian Westphal <fw@strlen.de>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/netlink.h>
14 #include <linux/slab.h>
16 #include <linux/netfilter.h>
18 #include <linux/netfilter/nfnetlink.h>
19 #include <linux/netfilter/nfnetlink_hook.h>
21 #include <net/netfilter/nf_tables.h>
24 static const struct nla_policy nfnl_hook_nla_policy[NFNLA_HOOK_MAX + 1] = {
25 [NFNLA_HOOK_HOOKNUM] = { .type = NLA_U32 },
26 [NFNLA_HOOK_PRIORITY] = { .type = NLA_U32 },
27 [NFNLA_HOOK_DEV] = { .type = NLA_STRING,
28 .len = IFNAMSIZ - 1 },
29 [NFNLA_HOOK_FUNCTION_NAME] = { .type = NLA_NUL_STRING,
30 .len = KSYM_NAME_LEN, },
31 [NFNLA_HOOK_MODULE_NAME] = { .type = NLA_NUL_STRING,
32 .len = MODULE_NAME_LEN, },
33 [NFNLA_HOOK_CHAIN_INFO] = { .type = NLA_NESTED, },
36 static int nf_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
37 const struct nlmsghdr *nlh,
38 struct netlink_dump_control *c)
42 if (!try_module_get(THIS_MODULE))
46 err = netlink_dump_start(nlsk, skb, nlh, c);
48 module_put(THIS_MODULE);
53 struct nfnl_dump_hook_data {
54 char devname[IFNAMSIZ];
59 static int nfnl_hook_put_nft_chain_info(struct sk_buff *nlskb,
60 const struct nfnl_dump_hook_data *ctx,
62 const struct nf_hook_ops *ops)
64 struct net *net = sock_net(nlskb->sk);
65 struct nlattr *nest, *nest2;
66 struct nft_chain *chain;
69 if (ops->hook_ops_type != NF_HOOK_OP_NF_TABLES)
73 if (WARN_ON_ONCE(!chain))
76 if (!nft_is_active(net, chain))
79 nest = nla_nest_start(nlskb, NFNLA_HOOK_CHAIN_INFO);
83 ret = nla_put_be32(nlskb, NFNLA_HOOK_INFO_TYPE,
84 htonl(NFNL_HOOK_TYPE_NFTABLES));
88 nest2 = nla_nest_start(nlskb, NFNLA_HOOK_INFO_DESC);
92 ret = nla_put_string(nlskb, NFNLA_CHAIN_TABLE, chain->table->name);
96 ret = nla_put_string(nlskb, NFNLA_CHAIN_NAME, chain->name);
100 ret = nla_put_u8(nlskb, NFNLA_CHAIN_FAMILY, chain->table->family);
104 nla_nest_end(nlskb, nest2);
105 nla_nest_end(nlskb, nest);
109 nla_nest_cancel(nlskb, nest);
113 static int nfnl_hook_dump_one(struct sk_buff *nlskb,
114 const struct nfnl_dump_hook_data *ctx,
115 const struct nf_hook_ops *ops,
116 int family, unsigned int seq)
118 u16 event = nfnl_msg_type(NFNL_SUBSYS_HOOK, NFNL_MSG_HOOK_GET);
119 unsigned int portid = NETLINK_CB(nlskb).portid;
120 struct nlmsghdr *nlh;
123 #ifdef CONFIG_KALLSYMS
124 char sym[KSYM_SYMBOL_LEN];
127 nlh = nfnl_msg_put(nlskb, portid, seq, event,
128 NLM_F_MULTI, family, NFNETLINK_V0, 0);
130 goto nla_put_failure;
132 #ifdef CONFIG_KALLSYMS
133 ret = snprintf(sym, sizeof(sym), "%ps", ops->hook);
134 if (ret >= sizeof(sym)) {
136 goto nla_put_failure;
139 module_name = strstr(sym, " [");
145 end = strchr(module_name, ']');
149 ret = nla_put_string(nlskb, NFNLA_HOOK_MODULE_NAME, module_name);
151 goto nla_put_failure;
155 ret = nla_put_string(nlskb, NFNLA_HOOK_FUNCTION_NAME, sym);
157 goto nla_put_failure;
160 if (ops->pf == NFPROTO_INET && ops->hooknum == NF_INET_INGRESS)
161 hooknum = NF_NETDEV_INGRESS;
163 hooknum = ops->hooknum;
165 ret = nla_put_be32(nlskb, NFNLA_HOOK_HOOKNUM, htonl(hooknum));
167 goto nla_put_failure;
169 ret = nla_put_be32(nlskb, NFNLA_HOOK_PRIORITY, htonl(ops->priority));
171 goto nla_put_failure;
173 ret = nfnl_hook_put_nft_chain_info(nlskb, ctx, seq, ops);
175 goto nla_put_failure;
177 nlmsg_end(nlskb, nlh);
180 nlmsg_trim(nlskb, nlh);
184 static const struct nf_hook_entries *
185 nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *dev)
187 const struct nf_hook_entries *hook_head = NULL;
188 #ifdef CONFIG_NETFILTER_INGRESS
189 struct net_device *netdev;
194 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv4))
195 return ERR_PTR(-EINVAL);
196 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
199 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv6))
200 return ERR_PTR(-EINVAL);
201 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
204 #ifdef CONFIG_NETFILTER_FAMILY_ARP
205 if (hook >= ARRAY_SIZE(net->nf.hooks_arp))
206 return ERR_PTR(-EINVAL);
207 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
211 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
212 if (hook >= ARRAY_SIZE(net->nf.hooks_bridge))
213 return ERR_PTR(-EINVAL);
214 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
217 #if IS_ENABLED(CONFIG_DECNET)
219 if (hook >= ARRAY_SIZE(net->nf.hooks_decnet))
220 return ERR_PTR(-EINVAL);
221 hook_head = rcu_dereference(net->nf.hooks_decnet[hook]);
224 #ifdef CONFIG_NETFILTER_INGRESS
226 if (hook != NF_NETDEV_INGRESS)
227 return ERR_PTR(-EOPNOTSUPP);
230 return ERR_PTR(-ENODEV);
232 netdev = dev_get_by_name_rcu(net, dev);
234 return ERR_PTR(-ENODEV);
236 return rcu_dereference(netdev->nf_hooks_ingress);
239 return ERR_PTR(-EPROTONOSUPPORT);
245 static int nfnl_hook_dump(struct sk_buff *nlskb,
246 struct netlink_callback *cb)
248 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
249 struct nfnl_dump_hook_data *ctx = cb->data;
250 int err, family = nfmsg->nfgen_family;
251 struct net *net = sock_net(nlskb->sk);
252 struct nf_hook_ops * const *ops;
253 const struct nf_hook_entries *e;
254 unsigned int i = cb->args[0];
258 e = nfnl_hook_entries_head(family, ctx->hook, net, ctx->devname);
267 if ((unsigned long)e != ctx->headv || i >= e->num_hook_entries)
270 ops = nf_hook_entries_get_hook_ops(e);
272 for (; i < e->num_hook_entries; i++) {
273 err = nfnl_hook_dump_one(nlskb, ctx, ops[i], family,
280 nl_dump_check_consistent(cb, nlmsg_hdr(nlskb));
286 static int nfnl_hook_dump_start(struct netlink_callback *cb)
288 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
289 const struct nlattr * const *nla = cb->data;
290 struct nfnl_dump_hook_data *ctx = NULL;
291 struct net *net = sock_net(cb->skb->sk);
292 u8 family = nfmsg->nfgen_family;
293 char name[IFNAMSIZ] = "";
297 hooknum = ntohl(nla_get_be32(nla[NFNLA_HOOK_HOOKNUM]));
301 if (family == NFPROTO_NETDEV) {
302 if (!nla[NFNLA_HOOK_DEV])
305 nla_strscpy(name, nla[NFNLA_HOOK_DEV], sizeof(name));
309 /* Not dereferenced; for consistency check only */
310 head = nfnl_hook_entries_head(family, hooknum, net, name);
313 if (head && IS_ERR(head))
314 return PTR_ERR(head);
316 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
320 strscpy(ctx->devname, name, sizeof(ctx->devname));
321 ctx->headv = (unsigned long)head;
330 static int nfnl_hook_dump_stop(struct netlink_callback *cb)
336 static int nfnl_hook_get(struct sk_buff *skb,
337 const struct nfnl_info *info,
338 const struct nlattr * const nla[])
340 if (!nla[NFNLA_HOOK_HOOKNUM])
343 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
344 struct netlink_dump_control c = {
345 .start = nfnl_hook_dump_start,
346 .done = nfnl_hook_dump_stop,
347 .dump = nfnl_hook_dump,
348 .module = THIS_MODULE,
352 return nf_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
358 static const struct nfnl_callback nfnl_hook_cb[NFNL_MSG_HOOK_MAX] = {
359 [NFNL_MSG_HOOK_GET] = {
360 .call = nfnl_hook_get,
362 .attr_count = NFNLA_HOOK_MAX,
363 .policy = nfnl_hook_nla_policy
367 static const struct nfnetlink_subsystem nfhook_subsys = {
369 .subsys_id = NFNL_SUBSYS_HOOK,
370 .cb_count = NFNL_MSG_HOOK_MAX,
374 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_HOOK);
376 static int __init nfnetlink_hook_init(void)
378 return nfnetlink_subsys_register(&nfhook_subsys);
381 static void __exit nfnetlink_hook_exit(void)
383 nfnetlink_subsys_unregister(&nfhook_subsys);
386 module_init(nfnetlink_hook_init);
387 module_exit(nfnetlink_hook_exit);
389 MODULE_LICENSE("GPL");
390 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
391 MODULE_DESCRIPTION("nfnetlink_hook: list registered netfilter hooks");