1
2
3
4
5
6
7
8#include <linux/module.h>
9#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter_arp/arp_tables.h>
11#include <linux/slab.h>
12
13MODULE_LICENSE("GPL");
14MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
15MODULE_DESCRIPTION("arptables filter table");
16
17#define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
18 (1 << NF_ARP_FORWARD))
19
20static int __net_init arptable_filter_table_init(struct net *net);
21
22static const struct xt_table packet_filter = {
23 .name = "filter",
24 .valid_hooks = FILTER_VALID_HOOKS,
25 .me = THIS_MODULE,
26 .af = NFPROTO_ARP,
27 .priority = NF_IP_PRI_FILTER,
28 .table_init = arptable_filter_table_init,
29};
30
31
32static unsigned int
33arptable_filter_hook(void *priv, struct sk_buff *skb,
34 const struct nf_hook_state *state)
35{
36 return arpt_do_table(skb, state, state->net->ipv4.arptable_filter);
37}
38
39static struct nf_hook_ops *arpfilter_ops __read_mostly;
40
41static int __net_init arptable_filter_table_init(struct net *net)
42{
43 struct arpt_replace *repl;
44 int err;
45
46 if (net->ipv4.arptable_filter)
47 return 0;
48
49 repl = arpt_alloc_initial_table(&packet_filter);
50 if (repl == NULL)
51 return -ENOMEM;
52 err = arpt_register_table(net, &packet_filter, repl, arpfilter_ops,
53 &net->ipv4.arptable_filter);
54 kfree(repl);
55 return err;
56}
57
58static void __net_exit arptable_filter_net_exit(struct net *net)
59{
60 if (!net->ipv4.arptable_filter)
61 return;
62 arpt_unregister_table(net, net->ipv4.arptable_filter, arpfilter_ops);
63 net->ipv4.arptable_filter = NULL;
64}
65
66static struct pernet_operations arptable_filter_net_ops = {
67 .exit = arptable_filter_net_exit,
68};
69
70static int __init arptable_filter_init(void)
71{
72 int ret;
73
74 arpfilter_ops = xt_hook_ops_alloc(&packet_filter, arptable_filter_hook);
75 if (IS_ERR(arpfilter_ops))
76 return PTR_ERR(arpfilter_ops);
77
78 ret = register_pernet_subsys(&arptable_filter_net_ops);
79 if (ret < 0) {
80 kfree(arpfilter_ops);
81 return ret;
82 }
83
84 ret = arptable_filter_table_init(&init_net);
85 if (ret) {
86 unregister_pernet_subsys(&arptable_filter_net_ops);
87 kfree(arpfilter_ops);
88 }
89
90 return ret;
91}
92
93static void __exit arptable_filter_fini(void)
94{
95 unregister_pernet_subsys(&arptable_filter_net_ops);
96 kfree(arpfilter_ops);
97}
98
99module_init(arptable_filter_init);
100module_exit(arptable_filter_fini);
101