projects/linux·blob master · refs

87 lines · 2.3 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2011 Patrick McHardy <[email protected]>
4 */
5
6#include <linux/module.h>
7#include <linux/skbuff.h>
8#include <linux/netdevice.h>
9
10#include <linux/netfilter/xt_devgroup.h>
11#include <linux/netfilter/x_tables.h>
12
13MODULE_AUTHOR("Patrick McHardy <[email protected]>");
14MODULE_LICENSE("GPL");
15MODULE_DESCRIPTION("Xtables: Device group match");
16MODULE_ALIAS("ipt_devgroup");
17MODULE_ALIAS("ip6t_devgroup");
18
19static bool devgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)
20{
21 const struct xt_devgroup_info *info = par->matchinfo;
22
23 if (info->flags & XT_DEVGROUP_MATCH_SRC &&
24 (((info->src_group ^ xt_in(par)->group) & info->src_mask ? 1 : 0) ^
25 ((info->flags & XT_DEVGROUP_INVERT_SRC) ? 1 : 0)))
26 return false;
27
28 if (info->flags & XT_DEVGROUP_MATCH_DST &&
29 (((info->dst_group ^ xt_out(par)->group) & info->dst_mask ? 1 : 0) ^
30 ((info->flags & XT_DEVGROUP_INVERT_DST) ? 1 : 0)))
31 return false;
32
33 return true;
34}
35
36static int devgroup_mt_check_hooks(const struct xt_mtchk_param *par)
37{
38 const struct xt_devgroup_info *info = par->matchinfo;
39
40 if (info->flags & XT_DEVGROUP_MATCH_SRC &&
41 par->hook_mask & ~((1 << NF_INET_PRE_ROUTING) |
42 (1 << NF_INET_LOCAL_IN) |
43 (1 << NF_INET_FORWARD)))
44 return -EINVAL;
45
46 if (info->flags & XT_DEVGROUP_MATCH_DST &&
47 par->hook_mask & ~((1 << NF_INET_FORWARD) |
48 (1 << NF_INET_LOCAL_OUT) |
49 (1 << NF_INET_POST_ROUTING)))
50 return -EINVAL;
51
52 return 0;
53}
54
55static int devgroup_mt_checkentry(const struct xt_mtchk_param *par)
56{
57 const struct xt_devgroup_info *info = par->matchinfo;
58
59 if (info->flags & ~(XT_DEVGROUP_MATCH_SRC | XT_DEVGROUP_INVERT_SRC |
60 XT_DEVGROUP_MATCH_DST | XT_DEVGROUP_INVERT_DST))
61 return -EINVAL;
62
63 return 0;
64}
65
66static struct xt_match devgroup_mt_reg __read_mostly = {
67 .name = "devgroup",
68 .match = devgroup_mt,
69 .check_hooks = devgroup_mt_check_hooks,
70 .checkentry = devgroup_mt_checkentry,
71 .matchsize = sizeof(struct xt_devgroup_info),
72 .family = NFPROTO_UNSPEC,
73 .me = THIS_MODULE
74};
75
76static int __init devgroup_mt_init(void)
77{
78 return xt_register_match(&devgroup_mt_reg);
79}
80
81static void __exit devgroup_mt_exit(void)
82{
83 xt_unregister_match(&devgroup_mt_reg);
84}
85
86module_init(devgroup_mt_init);
87module_exit(devgroup_mt_exit);