projects/linux·blob master · refs

515 lines · 11.8 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2021 Red Hat GmbH
4 *
5 * Author: Florian Westphal <[email protected]>
6 */
7
8#include <linux/bpf.h>
9#include <linux/module.h>
10#include <linux/kallsyms.h>
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/skbuff.h>
14#include <linux/errno.h>
15#include <linux/netlink.h>
16#include <linux/slab.h>
17
18#include <linux/netfilter.h>
19
20#include <linux/netfilter/nfnetlink.h>
21#include <linux/netfilter/nfnetlink_hook.h>
22
23#include <net/netfilter/nf_tables.h>
24#include <net/sock.h>
25
26static const struct nla_policy nfnl_hook_nla_policy[NFNLA_HOOK_MAX + 1] = {
27 [NFNLA_HOOK_HOOKNUM] = NLA_POLICY_MAX(NLA_BE32, 255),
28 [NFNLA_HOOK_PRIORITY] = { .type = NLA_U32 },
29 [NFNLA_HOOK_DEV] = { .type = NLA_STRING,
30 .len = IFNAMSIZ - 1 },
31 [NFNLA_HOOK_FUNCTION_NAME] = { .type = NLA_NUL_STRING,
32 .len = KSYM_NAME_LEN, },
33 [NFNLA_HOOK_MODULE_NAME] = { .type = NLA_NUL_STRING,
34 .len = MODULE_NAME_LEN, },
35 [NFNLA_HOOK_CHAIN_INFO] = { .type = NLA_NESTED, },
36};
37
38static int nf_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
39 const struct nlmsghdr *nlh,
40 struct netlink_dump_control *c)
41{
42 int err;
43
44 if (!try_module_get(THIS_MODULE))
45 return -EINVAL;
46
47 rcu_read_unlock();
48 err = netlink_dump_start(nlsk, skb, nlh, c);
49 rcu_read_lock();
50 module_put(THIS_MODULE);
51
52 return err;
53}
54
55struct nfnl_dump_hook_data {
56 char devname[IFNAMSIZ];
57 unsigned long headv;
58 u8 hook;
59};
60
61static struct nlattr *nfnl_start_info_type(struct sk_buff *nlskb, enum nfnl_hook_chaintype t)
62{
63 struct nlattr *nest = nla_nest_start(nlskb, NFNLA_HOOK_CHAIN_INFO);
64 int ret;
65
66 if (!nest)
67 return NULL;
68
69 ret = nla_put_be32(nlskb, NFNLA_HOOK_INFO_TYPE, htonl(t));
70 if (ret == 0)
71 return nest;
72
73 nla_nest_cancel(nlskb, nest);
74 return NULL;
75}
76
77static int nfnl_hook_put_bpf_prog_info(struct sk_buff *nlskb,
78 const struct nfnl_dump_hook_data *ctx,
79 unsigned int seq,
80 const struct bpf_prog *prog)
81{
82 struct nlattr *nest, *nest2;
83 int ret;
84
85 if (!IS_ENABLED(CONFIG_NETFILTER_BPF_LINK))
86 return 0;
87
88 if (WARN_ON_ONCE(!prog))
89 return 0;
90
91 nest = nfnl_start_info_type(nlskb, NFNL_HOOK_TYPE_BPF);
92 if (!nest)
93 return -EMSGSIZE;
94
95 nest2 = nla_nest_start(nlskb, NFNLA_HOOK_INFO_DESC);
96 if (!nest2)
97 goto cancel_nest;
98
99 ret = nla_put_be32(nlskb, NFNLA_HOOK_BPF_ID, htonl(prog->aux->id));
100 if (ret)
101 goto cancel_nest;
102
103 nla_nest_end(nlskb, nest2);
104 nla_nest_end(nlskb, nest);
105 return 0;
106
107cancel_nest:
108 nla_nest_cancel(nlskb, nest);
109 return -EMSGSIZE;
110}
111
112static int nfnl_hook_put_nft_info_desc(struct sk_buff *nlskb, const char *tname,
113 const char *name, u8 family)
114{
115 struct nlattr *nest;
116
117 nest = nla_nest_start(nlskb, NFNLA_HOOK_INFO_DESC);
118 if (!nest ||
119 nla_put_string(nlskb, NFNLA_CHAIN_TABLE, tname) ||
120 nla_put_string(nlskb, NFNLA_CHAIN_NAME, name) ||
121 nla_put_u8(nlskb, NFNLA_CHAIN_FAMILY, family)) {
122 nla_nest_cancel(nlskb, nest);
123 return -EMSGSIZE;
124 }
125 nla_nest_end(nlskb, nest);
126 return 0;
127}
128
129static int nfnl_hook_put_nft_chain_info(struct sk_buff *nlskb,
130 const struct nfnl_dump_hook_data *ctx,
131 unsigned int seq,
132 struct nft_chain *chain)
133{
134 struct net *net = sock_net(nlskb->sk);
135 struct nlattr *nest;
136 int ret = 0;
137
138 if (WARN_ON_ONCE(!chain))
139 return 0;
140
141 if (!nft_is_active(net, chain))
142 return 0;
143
144 nest = nfnl_start_info_type(nlskb, NFNL_HOOK_TYPE_NFTABLES);
145 if (!nest)
146 return -EMSGSIZE;
147
148 ret = nfnl_hook_put_nft_info_desc(nlskb, chain->table->name,
149 chain->name, chain->table->family);
150 if (ret) {
151 nla_nest_cancel(nlskb, nest);
152 return ret;
153 }
154
155 nla_nest_end(nlskb, nest);
156 return 0;
157}
158
159static int nfnl_hook_put_nft_ft_info(struct sk_buff *nlskb,
160 const struct nfnl_dump_hook_data *ctx,
161 unsigned int seq,
162 struct nf_flowtable *nf_ft)
163{
164 struct nft_flowtable *ft =
165 container_of(nf_ft, struct nft_flowtable, data);
166 struct net *net = sock_net(nlskb->sk);
167 struct nlattr *nest;
168 int ret = 0;
169
170 if (WARN_ON_ONCE(!nf_ft))
171 return 0;
172
173 if (!nft_is_active(net, ft))
174 return 0;
175
176 nest = nfnl_start_info_type(nlskb, NFNL_HOOK_TYPE_NFT_FLOWTABLE);
177 if (!nest)
178 return -EMSGSIZE;
179
180 ret = nfnl_hook_put_nft_info_desc(nlskb, ft->table->name,
181 ft->name, ft->table->family);
182 if (ret) {
183 nla_nest_cancel(nlskb, nest);
184 return ret;
185 }
186
187 nla_nest_end(nlskb, nest);
188 return 0;
189}
190
191static int nfnl_hook_dump_one(struct sk_buff *nlskb,
192 const struct nfnl_dump_hook_data *ctx,
193 const struct nf_hook_ops *ops, int priority,
194 int family, unsigned int seq)
195{
196 u16 event = nfnl_msg_type(NFNL_SUBSYS_HOOK, NFNL_MSG_HOOK_GET);
197 unsigned int portid = NETLINK_CB(nlskb).portid;
198 struct nlmsghdr *nlh;
199 int ret = -EMSGSIZE;
200 u32 hooknum;
201#ifdef CONFIG_KALLSYMS
202 char sym[KSYM_SYMBOL_LEN];
203 char *module_name;
204#endif
205 nlh = nfnl_msg_put(nlskb, portid, seq, event,
206 NLM_F_MULTI, family, NFNETLINK_V0, 0);
207 if (!nlh)
208 goto nla_put_failure;
209
210#ifdef CONFIG_KALLSYMS
211 ret = snprintf(sym, sizeof(sym), "%ps", ops->hook);
212 if (ret >= sizeof(sym)) {
213 ret = -EINVAL;
214 goto nla_put_failure;
215 }
216
217 module_name = strstr(sym, " [");
218 if (module_name) {
219 char *end;
220
221 *module_name = '\0';
222 module_name += 2;
223 end = strchr(module_name, ']');
224 if (end) {
225 *end = 0;
226
227 ret = nla_put_string(nlskb, NFNLA_HOOK_MODULE_NAME, module_name);
228 if (ret)
229 goto nla_put_failure;
230 }
231 }
232
233 ret = nla_put_string(nlskb, NFNLA_HOOK_FUNCTION_NAME, sym);
234 if (ret)
235 goto nla_put_failure;
236#endif
237
238 if (ops->pf == NFPROTO_INET && ops->hooknum == NF_INET_INGRESS)
239 hooknum = NF_NETDEV_INGRESS;
240 else
241 hooknum = ops->hooknum;
242
243 ret = nla_put_be32(nlskb, NFNLA_HOOK_HOOKNUM, htonl(hooknum));
244 if (ret)
245 goto nla_put_failure;
246
247 ret = nla_put_be32(nlskb, NFNLA_HOOK_PRIORITY, htonl(priority));
248 if (ret)
249 goto nla_put_failure;
250
251 switch (ops->hook_ops_type) {
252 case NF_HOOK_OP_NF_TABLES:
253 ret = nfnl_hook_put_nft_chain_info(nlskb, ctx, seq, ops->priv);
254 break;
255 case NF_HOOK_OP_BPF:
256 ret = nfnl_hook_put_bpf_prog_info(nlskb, ctx, seq, ops->priv);
257 break;
258 case NF_HOOK_OP_NFT_FT:
259 ret = nfnl_hook_put_nft_ft_info(nlskb, ctx, seq, ops->priv);
260 break;
261 case NF_HOOK_OP_UNDEFINED:
262 break;
263 default:
264 WARN_ON_ONCE(1);
265 break;
266 }
267
268 if (ret)
269 goto nla_put_failure;
270
271 nlmsg_end(nlskb, nlh);
272 return 0;
273nla_put_failure:
274 nlmsg_trim(nlskb, nlh);
275 return ret;
276}
277
278static const struct nf_hook_entries *
279nfnl_hook_entries_head(u8 pf, unsigned int hook, struct net *net, const char *dev)
280{
281 const struct nf_hook_entries *hook_head = NULL;
282#if defined(CONFIG_NETFILTER_INGRESS) || defined(CONFIG_NETFILTER_EGRESS)
283 struct net_device *netdev;
284#endif
285
286 switch (pf) {
287 case NFPROTO_IPV4:
288 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv4))
289 return ERR_PTR(-EINVAL);
290 hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
291 break;
292 case NFPROTO_IPV6:
293 if (hook >= ARRAY_SIZE(net->nf.hooks_ipv6))
294 return ERR_PTR(-EINVAL);
295 hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
296 break;
297 case NFPROTO_ARP:
298#ifdef CONFIG_NETFILTER_FAMILY_ARP
299 if (hook >= ARRAY_SIZE(net->nf.hooks_arp))
300 return ERR_PTR(-EINVAL);
301 hook_head = rcu_dereference(net->nf.hooks_arp[hook]);
302#endif
303 break;
304 case NFPROTO_BRIDGE:
305#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
306 if (hook >= ARRAY_SIZE(net->nf.hooks_bridge))
307 return ERR_PTR(-EINVAL);
308 hook_head = rcu_dereference(net->nf.hooks_bridge[hook]);
309#endif
310 break;
311#if defined(CONFIG_NETFILTER_INGRESS) || defined(CONFIG_NETFILTER_EGRESS)
312 case NFPROTO_NETDEV:
313 if (hook >= NF_NETDEV_NUMHOOKS)
314 return ERR_PTR(-EOPNOTSUPP);
315
316 if (!dev)
317 return ERR_PTR(-ENODEV);
318
319 netdev = dev_get_by_name_rcu(net, dev);
320 if (!netdev)
321 return ERR_PTR(-ENODEV);
322
323#ifdef CONFIG_NETFILTER_INGRESS
324 if (hook == NF_NETDEV_INGRESS)
325 return rcu_dereference(netdev->nf_hooks_ingress);
326#endif
327#ifdef CONFIG_NETFILTER_EGRESS
328 if (hook == NF_NETDEV_EGRESS)
329 return rcu_dereference(netdev->nf_hooks_egress);
330#endif
331 fallthrough;
332#endif
333 default:
334 return ERR_PTR(-EPROTONOSUPPORT);
335 }
336
337 return hook_head;
338}
339
340static int nfnl_hook_dump_nat(struct sk_buff *nlskb,
341 const struct nfnl_dump_hook_data *ctx,
342 const struct nf_hook_ops *ops,
343 int family, unsigned int seq)
344{
345 struct nf_nat_lookup_hook_priv *priv = ops->priv;
346 struct nf_hook_entries *e = rcu_dereference(priv->entries);
347 struct nf_hook_ops **nat_ops;
348 int i, err;
349
350 if (!e)
351 return 0;
352
353 nat_ops = nf_hook_entries_get_hook_ops(e);
354
355 for (i = 0; i < e->num_hook_entries; i++) {
356 err = nfnl_hook_dump_one(nlskb, ctx, nat_ops[i],
357 ops->priority, family, seq);
358 if (err)
359 return err;
360 }
361 return 0;
362}
363
364static int nfnl_hook_dump(struct sk_buff *nlskb,
365 struct netlink_callback *cb)
366{
367 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
368 struct nfnl_dump_hook_data *ctx = cb->data;
369 int err, family = nfmsg->nfgen_family;
370 struct net *net = sock_net(nlskb->sk);
371 struct nf_hook_ops * const *ops;
372 const struct nf_hook_entries *e;
373 unsigned int i = cb->args[0];
374
375 rcu_read_lock();
376
377 e = nfnl_hook_entries_head(family, ctx->hook, net, ctx->devname);
378 if (!e)
379 goto done;
380
381 if (IS_ERR(e)) {
382 cb->seq++;
383 goto done;
384 }
385
386 if ((unsigned long)e != ctx->headv || i >= e->num_hook_entries)
387 cb->seq++;
388
389 ops = nf_hook_entries_get_hook_ops(e);
390
391 for (; i < e->num_hook_entries; i++) {
392 if (ops[i]->hook_ops_type == NF_HOOK_OP_NAT)
393 err = nfnl_hook_dump_nat(nlskb, ctx, ops[i], family,
394 cb->nlh->nlmsg_seq);
395 else
396 err = nfnl_hook_dump_one(nlskb, ctx, ops[i],
397 ops[i]->priority, family,
398 cb->nlh->nlmsg_seq);
399 if (err)
400 break;
401 }
402
403done:
404 nl_dump_check_consistent(cb, nlmsg_hdr(nlskb));
405 rcu_read_unlock();
406 cb->args[0] = i;
407 return nlskb->len;
408}
409
410static int nfnl_hook_dump_start(struct netlink_callback *cb)
411{
412 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
413 const struct nlattr * const *nla = cb->data;
414 struct nfnl_dump_hook_data *ctx = NULL;
415 struct net *net = sock_net(cb->skb->sk);
416 u8 family = nfmsg->nfgen_family;
417 char name[IFNAMSIZ] = "";
418 const void *head;
419 u32 hooknum;
420
421 hooknum = ntohl(nla_get_be32(nla[NFNLA_HOOK_HOOKNUM]));
422 if (hooknum > 255)
423 return -EINVAL;
424
425 if (family == NFPROTO_NETDEV) {
426 if (!nla[NFNLA_HOOK_DEV])
427 return -EINVAL;
428
429 nla_strscpy(name, nla[NFNLA_HOOK_DEV], sizeof(name));
430 }
431
432 rcu_read_lock();
433 /* Not dereferenced; for consistency check only */
434 head = nfnl_hook_entries_head(family, hooknum, net, name);
435 rcu_read_unlock();
436
437 if (head && IS_ERR(head))
438 return PTR_ERR(head);
439
440 ctx = kzalloc_obj(*ctx);
441 if (!ctx)
442 return -ENOMEM;
443
444 strscpy(ctx->devname, name, sizeof(ctx->devname));
445 ctx->headv = (unsigned long)head;
446 ctx->hook = hooknum;
447
448 cb->seq = 1;
449 cb->data = ctx;
450
451 return 0;
452}
453
454static int nfnl_hook_dump_stop(struct netlink_callback *cb)
455{
456 kfree(cb->data);
457 return 0;
458}
459
460static int nfnl_hook_get(struct sk_buff *skb,
461 const struct nfnl_info *info,
462 const struct nlattr * const nla[])
463{
464 if (!nla[NFNLA_HOOK_HOOKNUM])
465 return -EINVAL;
466
467 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
468 struct netlink_dump_control c = {
469 .start = nfnl_hook_dump_start,
470 .done = nfnl_hook_dump_stop,
471 .dump = nfnl_hook_dump,
472 .module = THIS_MODULE,
473 .data = (void *)nla,
474 };
475
476 return nf_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c);
477 }
478
479 return -EOPNOTSUPP;
480}
481
482static const struct nfnl_callback nfnl_hook_cb[NFNL_MSG_HOOK_MAX] = {
483 [NFNL_MSG_HOOK_GET] = {
484 .call = nfnl_hook_get,
485 .type = NFNL_CB_RCU,
486 .attr_count = NFNLA_HOOK_MAX,
487 .policy = nfnl_hook_nla_policy
488 },
489};
490
491static const struct nfnetlink_subsystem nfhook_subsys = {
492 .name = "nfhook",
493 .subsys_id = NFNL_SUBSYS_HOOK,
494 .cb_count = NFNL_MSG_HOOK_MAX,
495 .cb = nfnl_hook_cb,
496};
497
498MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_HOOK);
499
500static int __init nfnetlink_hook_init(void)
501{
502 return nfnetlink_subsys_register(&nfhook_subsys);
503}
504
505static void __exit nfnetlink_hook_exit(void)
506{
507 nfnetlink_subsys_unregister(&nfhook_subsys);
508}
509
510module_init(nfnetlink_hook_init);
511module_exit(nfnetlink_hook_exit);
512
513MODULE_LICENSE("GPL");
514MODULE_AUTHOR("Florian Westphal <[email protected]>");
515MODULE_DESCRIPTION("nfnetlink_hook: list registered netfilter hooks");