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