1
2
3
4
5
6
7
8
9
10
11#include <linux/netfilter_bridge/ebtables.h>
12#include <linux/module.h>
13
14#define FILTER_VALID_HOOKS ((1 << NF_BR_LOCAL_IN) | (1 << NF_BR_FORWARD) | \
15 (1 << NF_BR_LOCAL_OUT))
16
17static struct ebt_entries initial_chains[] =
18{
19 {
20 .name = "INPUT",
21 .policy = EBT_ACCEPT,
22 },
23 {
24 .name = "FORWARD",
25 .policy = EBT_ACCEPT,
26 },
27 {
28 .name = "OUTPUT",
29 .policy = EBT_ACCEPT,
30 },
31};
32
33static struct ebt_replace_kernel initial_table =
34{
35 .name = "filter",
36 .valid_hooks = FILTER_VALID_HOOKS,
37 .entries_size = 3 * sizeof(struct ebt_entries),
38 .hook_entry = {
39 [NF_BR_LOCAL_IN] = &initial_chains[0],
40 [NF_BR_FORWARD] = &initial_chains[1],
41 [NF_BR_LOCAL_OUT] = &initial_chains[2],
42 },
43 .entries = (char *)initial_chains,
44};
45
46static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
47{
48 if (valid_hooks & ~FILTER_VALID_HOOKS)
49 return -EINVAL;
50 return 0;
51}
52
53static const struct ebt_table frame_filter =
54{
55 .name = "filter",
56 .table = &initial_table,
57 .valid_hooks = FILTER_VALID_HOOKS,
58 .check = check,
59 .me = THIS_MODULE,
60};
61
62static unsigned int
63ebt_in_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
64 const struct net_device *in, const struct net_device *out,
65 int (*okfn)(struct sk_buff *))
66{
67 return ebt_do_table(ops->hooknum, skb, in, out,
68 dev_net(in)->xt.frame_filter);
69}
70
71static unsigned int
72ebt_out_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
73 const struct net_device *in, const struct net_device *out,
74 int (*okfn)(struct sk_buff *))
75{
76 return ebt_do_table(ops->hooknum, skb, in, out,
77 dev_net(out)->xt.frame_filter);
78}
79
80static struct nf_hook_ops ebt_ops_filter[] __read_mostly = {
81 {
82 .hook = ebt_in_hook,
83 .owner = THIS_MODULE,
84 .pf = NFPROTO_BRIDGE,
85 .hooknum = NF_BR_LOCAL_IN,
86 .priority = NF_BR_PRI_FILTER_BRIDGED,
87 },
88 {
89 .hook = ebt_in_hook,
90 .owner = THIS_MODULE,
91 .pf = NFPROTO_BRIDGE,
92 .hooknum = NF_BR_FORWARD,
93 .priority = NF_BR_PRI_FILTER_BRIDGED,
94 },
95 {
96 .hook = ebt_out_hook,
97 .owner = THIS_MODULE,
98 .pf = NFPROTO_BRIDGE,
99 .hooknum = NF_BR_LOCAL_OUT,
100 .priority = NF_BR_PRI_FILTER_OTHER,
101 },
102};
103
104static int __net_init frame_filter_net_init(struct net *net)
105{
106 net->xt.frame_filter = ebt_register_table(net, &frame_filter);
107 return PTR_ERR_OR_ZERO(net->xt.frame_filter);
108}
109
110static void __net_exit frame_filter_net_exit(struct net *net)
111{
112 ebt_unregister_table(net, net->xt.frame_filter);
113}
114
115static struct pernet_operations frame_filter_net_ops = {
116 .init = frame_filter_net_init,
117 .exit = frame_filter_net_exit,
118};
119
120static int __init ebtable_filter_init(void)
121{
122 int ret;
123
124 ret = register_pernet_subsys(&frame_filter_net_ops);
125 if (ret < 0)
126 return ret;
127 ret = nf_register_hooks(ebt_ops_filter, ARRAY_SIZE(ebt_ops_filter));
128 if (ret < 0)
129 unregister_pernet_subsys(&frame_filter_net_ops);
130 return ret;
131}
132
133static void __exit ebtable_filter_fini(void)
134{
135 nf_unregister_hooks(ebt_ops_filter, ARRAY_SIZE(ebt_ops_filter));
136 unregister_pernet_subsys(&frame_filter_net_ops);
137}
138
139module_init(ebtable_filter_init);
140module_exit(ebtable_filter_fini);
141MODULE_LICENSE("GPL");
142