GNU Linux-libre 4.19.207-gnu1
[releases.git] / net / bridge / netfilter / ebtable_filter.c
1 /*
2  *  ebtable_filter
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *
7  *  April, 2002
8  *
9  */
10
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <uapi/linux/netfilter_bridge.h>
13 #include <linux/module.h>
14
15 #define FILTER_VALID_HOOKS ((1 << NF_BR_LOCAL_IN) | (1 << NF_BR_FORWARD) | \
16                             (1 << NF_BR_LOCAL_OUT))
17
18 static struct ebt_entries initial_chains[] = {
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
33 static struct ebt_replace_kernel initial_table = {
34         .name           = "filter",
35         .valid_hooks    = FILTER_VALID_HOOKS,
36         .entries_size   = 3 * sizeof(struct ebt_entries),
37         .hook_entry     = {
38                 [NF_BR_LOCAL_IN]        = &initial_chains[0],
39                 [NF_BR_FORWARD]         = &initial_chains[1],
40                 [NF_BR_LOCAL_OUT]       = &initial_chains[2],
41         },
42         .entries        = (char *)initial_chains,
43 };
44
45 static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
46 {
47         if (valid_hooks & ~FILTER_VALID_HOOKS)
48                 return -EINVAL;
49         return 0;
50 }
51
52 static const struct ebt_table frame_filter = {
53         .name           = "filter",
54         .table          = &initial_table,
55         .valid_hooks    = FILTER_VALID_HOOKS,
56         .check          = check,
57         .me             = THIS_MODULE,
58 };
59
60 static unsigned int
61 ebt_in_hook(void *priv, struct sk_buff *skb,
62             const struct nf_hook_state *state)
63 {
64         return ebt_do_table(skb, state, state->net->xt.frame_filter);
65 }
66
67 static unsigned int
68 ebt_out_hook(void *priv, struct sk_buff *skb,
69              const struct nf_hook_state *state)
70 {
71         return ebt_do_table(skb, state, state->net->xt.frame_filter);
72 }
73
74 static const struct nf_hook_ops ebt_ops_filter[] = {
75         {
76                 .hook           = ebt_in_hook,
77                 .pf             = NFPROTO_BRIDGE,
78                 .hooknum        = NF_BR_LOCAL_IN,
79                 .priority       = NF_BR_PRI_FILTER_BRIDGED,
80         },
81         {
82                 .hook           = ebt_in_hook,
83                 .pf             = NFPROTO_BRIDGE,
84                 .hooknum        = NF_BR_FORWARD,
85                 .priority       = NF_BR_PRI_FILTER_BRIDGED,
86         },
87         {
88                 .hook           = ebt_out_hook,
89                 .pf             = NFPROTO_BRIDGE,
90                 .hooknum        = NF_BR_LOCAL_OUT,
91                 .priority       = NF_BR_PRI_FILTER_OTHER,
92         },
93 };
94
95 static int __net_init frame_filter_net_init(struct net *net)
96 {
97         return ebt_register_table(net, &frame_filter, ebt_ops_filter,
98                                   &net->xt.frame_filter);
99 }
100
101 static void __net_exit frame_filter_net_exit(struct net *net)
102 {
103         ebt_unregister_table(net, net->xt.frame_filter, ebt_ops_filter);
104 }
105
106 static struct pernet_operations frame_filter_net_ops = {
107         .init = frame_filter_net_init,
108         .exit = frame_filter_net_exit,
109 };
110
111 static int __init ebtable_filter_init(void)
112 {
113         return register_pernet_subsys(&frame_filter_net_ops);
114 }
115
116 static void __exit ebtable_filter_fini(void)
117 {
118         unregister_pernet_subsys(&frame_filter_net_ops);
119 }
120
121 module_init(ebtable_filter_init);
122 module_exit(ebtable_filter_fini);
123 MODULE_LICENSE("GPL");