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/kallsyms.h>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/errno.h>
14 #include <linux/netlink.h>
15 #include <linux/slab.h>
17 #include <linux/netfilter.h>
19 #include <linux/netfilter/nfnetlink.h>
20 #include <linux/netfilter/nfnetlink_hook.h>
22 #include <net/netfilter/nf_tables.h>
25 static const struct nla_policy nfnl_hook_nla_policy[NFNLA_HOOK_MAX + 1] = {
26 [NFNLA_HOOK_HOOKNUM] = { .type = NLA_U32 },
27 [NFNLA_HOOK_PRIORITY] = { .type = NLA_U32 },
28 [NFNLA_HOOK_DEV] = { .type = NLA_STRING,
29 .len = IFNAMSIZ - 1 },
30 [NFNLA_HOOK_FUNCTION_NAME] = { .type = NLA_NUL_STRING,
31 .len = KSYM_NAME_LEN, },
32 [NFNLA_HOOK_MODULE_NAME] = { .type = NLA_NUL_STRING,
33 .len = MODULE_NAME_LEN, },
34 [NFNLA_HOOK_CHAIN_INFO] = { .type = NLA_NESTED, },
37 static int nf_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
38 const struct nlmsghdr *nlh,
39 struct netlink_dump_control *c)
43 if (!try_module_get(THIS_MODULE))
47 err = netlink_dump_start(nlsk, skb, nlh, c);
49 module_put(THIS_MODULE);
54 struct nfnl_dump_hook_data {
55 char devname[IFNAMSIZ];
60 static int nfnl_hook_put_nft_chain_info(struct sk_buff *nlskb,
61 const struct nfnl_dump_hook_data *ctx,
63 const struct nf_hook_ops *ops)
65 struct net *net = sock_net(nlskb->sk);
66 struct nlattr *nest, *nest2;
67 struct nft_chain *chain;
70 if (ops->hook_ops_type != NF_HOOK_OP_NF_TABLES)
74 if (WARN_ON_ONCE(!chain))
77 if (!nft_is_active(net, chain))
80 nest = nla_nest_start(nlskb, NFNLA_HOOK_CHAIN_INFO);
84 ret = nla_put_be32(nlskb, NFNLA_HOOK_INFO_TYPE,
85 htonl(NFNL_HOOK_TYPE_NFTABLES));
89 nest2 = nla_nest_start(nlskb, NFNLA_HOOK_INFO_DESC);
93 ret = nla_put_string(nlskb, NFNLA_CHAIN_TABLE, chain->table->name);
97 ret = nla_put_string(nlskb, NFNLA_CHAIN_NAME, chain->name);
101 ret = nla_put_u8(nlskb, NFNLA_CHAIN_FAMILY, chain->table->family);
105 nla_nest_end(nlskb, nest2);
106 nla_nest_end(nlskb, nest);
110 nla_nest_cancel(nlskb, nest);
114 static int nfnl_hook_dump_one(struct sk_buff *nlskb,
115 const struct nfnl_dump_hook_data *ctx,
116 const struct nf_hook_ops *ops,
117 int family, unsigned int seq)
119 u16 event = nfnl_msg_type(NFNL_SUBSYS_HOOK, NFNL_MSG_HOOK_GET);
120 unsigned int portid = NETLINK_CB(nlskb).portid;
121 struct nlmsghdr *nlh;
124 #ifdef CONFIG_KALLSYMS
125 char sym[KSYM_SYMBOL_LEN];
128 nlh = nfnl_msg_put(nlskb, portid, seq, event,
129 NLM_F_MULTI, family, NFNETLINK_V0, 0);
131 goto nla_put_failure;
133 #ifdef CONFIG_KALLSYMS
134 ret = snprintf(sym, sizeof(sym), "%ps", ops->hook);
135 if (ret >= sizeof(sym)) {
137 goto nla_put_failure;
140 module_name = strstr(sym, " [");
146 end = strchr(module_name, ']');
150 ret = nla_put_string(nlskb, NFNLA_HOOK_MODULE_NAME, module_name);
152 goto nla_put_failure;
156 ret = nla_put_string(nlskb, NFNLA_HOOK_FUNCTION_NAME, sym);
158 goto nla_put_failure;
161 if (ops->pf == NFPROTO_INET && ops->hooknum == NF_INET_INGRESS)
162 hooknum = NF_NETDEV_INGRESS;
164 hooknum = ops->hooknum;
166 ret = nla_put_be32(nlskb, NFNLA_HOOK_HOOKNUM, htonl(hooknum));
168 goto nla_put_failure;
170 ret = nla_put_be32(nlskb, NFNLA_HOOK_PRIORITY, htonl(ops->priority));
172 goto nla_put_failure;
174 ret = nfnl_hook_put_nft_chain_info(nlskb, ctx, seq, ops);
176 goto nla_put_failure;
178 nlmsg_end(nlskb, nlh);
181 nlmsg_trim(nlskb, nlh);
185 static const struct nf_hook_entries *
186 nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *dev)
188 const struct nf_hook_entries *hook_head = NULL;
189 #if defined(CONFIG_NETFILTER_INGRESS) || defined(CONFIG_NETFILTER_EGRESS)
190 struct net_device *netdev;
195 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv4))
196 return ERR_PTR(-EINVAL);
197 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
200 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv6))
201 return ERR_PTR(-EINVAL);
202 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
205 #ifdef CONFIG_NETFILTER_FAMILY_ARP
206 if (hook >= ARRAY_SIZE(net->nf.hooks_arp))
207 return ERR_PTR(-EINVAL);
208 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
212 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
213 if (hook >= ARRAY_SIZE(net->nf.hooks_bridge))
214 return ERR_PTR(-EINVAL);
215 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
218 #if defined(CONFIG_NETFILTER_INGRESS) || defined(CONFIG_NETFILTER_EGRESS)
220 if (hook >= NF_NETDEV_NUMHOOKS)
221 return ERR_PTR(-EOPNOTSUPP);
224 return ERR_PTR(-ENODEV);
226 netdev = dev_get_by_name_rcu(net, dev);
228 return ERR_PTR(-ENODEV);
230 #ifdef CONFIG_NETFILTER_INGRESS
231 if (hook == NF_NETDEV_INGRESS)
232 return rcu_dereference(netdev->nf_hooks_ingress);
234 #ifdef CONFIG_NETFILTER_EGRESS
235 if (hook == NF_NETDEV_EGRESS)
236 return rcu_dereference(netdev->nf_hooks_egress);
241 return ERR_PTR(-EPROTONOSUPPORT);
247 static int nfnl_hook_dump(struct sk_buff *nlskb,
248 struct netlink_callback *cb)
250 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
251 struct nfnl_dump_hook_data *ctx = cb->data;
252 int err, family = nfmsg->nfgen_family;
253 struct net *net = sock_net(nlskb->sk);
254 struct nf_hook_ops * const *ops;
255 const struct nf_hook_entries *e;
256 unsigned int i = cb->args[0];
260 e = nfnl_hook_entries_head(family, ctx->hook, net, ctx->devname);
269 if ((unsigned long)e != ctx->headv || i >= e->num_hook_entries)
272 ops = nf_hook_entries_get_hook_ops(e);
274 for (; i < e->num_hook_entries; i++) {
275 err = nfnl_hook_dump_one(nlskb, ctx, ops[i], family,
282 nl_dump_check_consistent(cb, nlmsg_hdr(nlskb));
288 static int nfnl_hook_dump_start(struct netlink_callback *cb)
290 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
291 const struct nlattr * const *nla = cb->data;
292 struct nfnl_dump_hook_data *ctx = NULL;
293 struct net *net = sock_net(cb->skb->sk);
294 u8 family = nfmsg->nfgen_family;
295 char name[IFNAMSIZ] = "";
299 hooknum = ntohl(nla_get_be32(nla[NFNLA_HOOK_HOOKNUM]));
303 if (family == NFPROTO_NETDEV) {
304 if (!nla[NFNLA_HOOK_DEV])
307 nla_strscpy(name, nla[NFNLA_HOOK_DEV], sizeof(name));
311 /* Not dereferenced; for consistency check only */
312 head = nfnl_hook_entries_head(family, hooknum, net, name);
315 if (head && IS_ERR(head))
316 return PTR_ERR(head);
318 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
322 strscpy(ctx->devname, name, sizeof(ctx->devname));
323 ctx->headv = (unsigned long)head;
332 static int nfnl_hook_dump_stop(struct netlink_callback *cb)
338 static int nfnl_hook_get(struct sk_buff *skb,
339 const struct nfnl_info *info,
340 const struct nlattr * const nla[])
342 if (!nla[NFNLA_HOOK_HOOKNUM])
345 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
346 struct netlink_dump_control c = {
347 .start = nfnl_hook_dump_start,
348 .done = nfnl_hook_dump_stop,
349 .dump = nfnl_hook_dump,
350 .module = THIS_MODULE,
354 return nf_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
360 static const struct nfnl_callback nfnl_hook_cb[NFNL_MSG_HOOK_MAX] = {
361 [NFNL_MSG_HOOK_GET] = {
362 .call = nfnl_hook_get,
364 .attr_count = NFNLA_HOOK_MAX,
365 .policy = nfnl_hook_nla_policy
369 static const struct nfnetlink_subsystem nfhook_subsys = {
371 .subsys_id = NFNL_SUBSYS_HOOK,
372 .cb_count = NFNL_MSG_HOOK_MAX,
376 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_HOOK);
378 static int __init nfnetlink_hook_init(void)
380 return nfnetlink_subsys_register(&nfhook_subsys);
383 static void __exit nfnetlink_hook_exit(void)
385 nfnetlink_subsys_unregister(&nfhook_subsys);
388 module_init(nfnetlink_hook_init);
389 module_exit(nfnetlink_hook_exit);
391 MODULE_LICENSE("GPL");
392 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
393 MODULE_DESCRIPTION("nfnetlink_hook: list registered netfilter hooks");