blob: bfe26f32b93069fc478cc559973b0b2cf743154f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Filtering ARP tables module.
3 *
4 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5 *
6 */
7
8#include <linux/module.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +02009#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/netfilter_arp/arp_tables.h>
11
12MODULE_LICENSE("GPL");
13MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
14MODULE_DESCRIPTION("arptables filter table");
15
16#define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
17 (1 << NF_ARP_FORWARD))
18
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020019static const struct xt_table packet_filter = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 .name = "filter",
21 .valid_hooks = FILTER_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 .me = THIS_MODULE,
Jan Engelhardtee999d82008-10-08 11:35:01 +020023 .af = NFPROTO_ARP,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020024 .priority = NF_IP_PRI_FILTER,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27/* The work comes in here from netfilter.c */
Jan Engelhardt737535c2009-06-13 06:46:36 +020028static unsigned int
29arptable_filter_hook(unsigned int hook, struct sk_buff *skb,
30 const struct net_device *in, const struct net_device *out,
31 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Jan Engelhardt2b21e052009-06-13 06:57:10 +020033 const struct net *net = dev_net((in != NULL) ? in : out);
Jan Engelhardt737535c2009-06-13 06:46:36 +020034
Jan Engelhardt2b21e052009-06-13 06:57:10 +020035 return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter);
Alexey Dobriyan3918fed52008-07-26 17:48:59 -070036}
37
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020038static struct nf_hook_ops *arpfilter_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080040static int __net_init arptable_filter_net_init(struct net *net)
41{
Jan Engelhardte3eaa992009-06-17 22:14:54 +020042 struct arpt_replace *repl;
43
44 repl = arpt_alloc_initial_table(&packet_filter);
45 if (repl == NULL)
46 return -ENOMEM;
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080047 net->ipv4.arptable_filter =
Jan Engelhardte3eaa992009-06-17 22:14:54 +020048 arpt_register_table(net, &packet_filter, repl);
49 kfree(repl);
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080050 if (IS_ERR(net->ipv4.arptable_filter))
51 return PTR_ERR(net->ipv4.arptable_filter);
52 return 0;
53}
54
55static void __net_exit arptable_filter_net_exit(struct net *net)
56{
57 arpt_unregister_table(net->ipv4.arptable_filter);
58}
59
60static struct pernet_operations arptable_filter_net_ops = {
61 .init = arptable_filter_net_init,
62 .exit = arptable_filter_net_exit,
63};
64
Andrew Morton65b4b4e2006-03-28 16:37:06 -080065static int __init arptable_filter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Patrick McHardy964ddaa2006-04-06 14:09:49 -070067 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080069 ret = register_pernet_subsys(&arptable_filter_net_ops);
70 if (ret < 0)
71 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020073 arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook);
74 if (IS_ERR(arpfilter_ops)) {
75 ret = PTR_ERR(arpfilter_ops);
Patrick McHardy964ddaa2006-04-06 14:09:49 -070076 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return ret;
79
Patrick McHardy964ddaa2006-04-06 14:09:49 -070080cleanup_table:
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080081 unregister_pernet_subsys(&arptable_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return ret;
83}
84
Andrew Morton65b4b4e2006-03-28 16:37:06 -080085static void __exit arptable_filter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020087 xt_hook_unlink(&packet_filter, arpfilter_ops);
Alexey Dobriyan9ea0cb22008-01-31 04:05:09 -080088 unregister_pernet_subsys(&arptable_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Andrew Morton65b4b4e2006-03-28 16:37:06 -080091module_init(arptable_filter_init);
92module_exit(arptable_filter_fini);