Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[linux-2.6] / net / ipv4 / netfilter / arptable_filter.c
1 /*
2  * Filtering ARP tables module.
3  *
4  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
5  *
6  */
7
8 #include <linux/module.h>
9 #include <linux/netfilter_arp/arp_tables.h>
10
11 MODULE_LICENSE("GPL");
12 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
13 MODULE_DESCRIPTION("arptables filter table");
14
15 #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
16                            (1 << NF_ARP_FORWARD))
17
18 static struct
19 {
20         struct arpt_replace repl;
21         struct arpt_standard entries[3];
22         struct arpt_error term;
23 } initial_table __net_initdata = {
24         .repl = {
25                 .name = "filter",
26                 .valid_hooks = FILTER_VALID_HOOKS,
27                 .num_entries = 4,
28                 .size = sizeof(struct arpt_standard) * 3 + sizeof(struct arpt_error),
29                 .hook_entry = {
30                         [NF_ARP_IN] = 0,
31                         [NF_ARP_OUT] = sizeof(struct arpt_standard),
32                         [NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard),
33                 },
34                 .underflow = {
35                         [NF_ARP_IN] = 0,
36                         [NF_ARP_OUT] = sizeof(struct arpt_standard),
37                         [NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard),
38                 },
39         },
40         .entries = {
41                 ARPT_STANDARD_INIT(NF_ACCEPT),  /* ARP_IN */
42                 ARPT_STANDARD_INIT(NF_ACCEPT),  /* ARP_OUT */
43                 ARPT_STANDARD_INIT(NF_ACCEPT),  /* ARP_FORWARD */
44         },
45         .term = ARPT_ERROR_INIT,
46 };
47
48 static struct xt_table packet_filter = {
49         .name           = "filter",
50         .valid_hooks    = FILTER_VALID_HOOKS,
51         .me             = THIS_MODULE,
52         .af             = NFPROTO_ARP,
53 };
54
55 /* The work comes in here from netfilter.c */
56 static unsigned int arpt_in_hook(unsigned int hook,
57                                  struct sk_buff *skb,
58                                  const struct net_device *in,
59                                  const struct net_device *out,
60                                  int (*okfn)(struct sk_buff *))
61 {
62         return arpt_do_table(skb, hook, in, out,
63                              dev_net(in)->ipv4.arptable_filter);
64 }
65
66 static unsigned int arpt_out_hook(unsigned int hook,
67                                   struct sk_buff *skb,
68                                   const struct net_device *in,
69                                   const struct net_device *out,
70                                   int (*okfn)(struct sk_buff *))
71 {
72         return arpt_do_table(skb, hook, in, out,
73                              dev_net(out)->ipv4.arptable_filter);
74 }
75
76 static struct nf_hook_ops arpt_ops[] __read_mostly = {
77         {
78                 .hook           = arpt_in_hook,
79                 .owner          = THIS_MODULE,
80                 .pf             = NFPROTO_ARP,
81                 .hooknum        = NF_ARP_IN,
82                 .priority       = NF_IP_PRI_FILTER,
83         },
84         {
85                 .hook           = arpt_out_hook,
86                 .owner          = THIS_MODULE,
87                 .pf             = NFPROTO_ARP,
88                 .hooknum        = NF_ARP_OUT,
89                 .priority       = NF_IP_PRI_FILTER,
90         },
91         {
92                 .hook           = arpt_in_hook,
93                 .owner          = THIS_MODULE,
94                 .pf             = NFPROTO_ARP,
95                 .hooknum        = NF_ARP_FORWARD,
96                 .priority       = NF_IP_PRI_FILTER,
97         },
98 };
99
100 static int __net_init arptable_filter_net_init(struct net *net)
101 {
102         /* Register table */
103         net->ipv4.arptable_filter =
104                 arpt_register_table(net, &packet_filter, &initial_table.repl);
105         if (IS_ERR(net->ipv4.arptable_filter))
106                 return PTR_ERR(net->ipv4.arptable_filter);
107         return 0;
108 }
109
110 static void __net_exit arptable_filter_net_exit(struct net *net)
111 {
112         arpt_unregister_table(net->ipv4.arptable_filter);
113 }
114
115 static struct pernet_operations arptable_filter_net_ops = {
116         .init = arptable_filter_net_init,
117         .exit = arptable_filter_net_exit,
118 };
119
120 static int __init arptable_filter_init(void)
121 {
122         int ret;
123
124         ret = register_pernet_subsys(&arptable_filter_net_ops);
125         if (ret < 0)
126                 return ret;
127
128         ret = nf_register_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
129         if (ret < 0)
130                 goto cleanup_table;
131         return ret;
132
133 cleanup_table:
134         unregister_pernet_subsys(&arptable_filter_net_ops);
135         return ret;
136 }
137
138 static void __exit arptable_filter_fini(void)
139 {
140         nf_unregister_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
141         unregister_pernet_subsys(&arptable_filter_net_ops);
142 }
143
144 module_init(arptable_filter_init);
145 module_exit(arptable_filter_fini);