netfilter: x_tables: use NFPROTO_* in extensions
[linux-2.6.git] / 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         .lock           = __RW_LOCK_UNLOCKED(packet_filter.lock),
52         .private        = NULL,
53         .me             = THIS_MODULE,
54         .af             = NFPROTO_ARP,
55 };
56
57 /* The work comes in here from netfilter.c */
58 static unsigned int arpt_in_hook(unsigned int hook,
59                                  struct sk_buff *skb,
60                                  const struct net_device *in,
61                                  const struct net_device *out,
62                                  int (*okfn)(struct sk_buff *))
63 {
64         return arpt_do_table(skb, hook, in, out,
65                              dev_net(in)->ipv4.arptable_filter);
66 }
67
68 static unsigned int arpt_out_hook(unsigned int hook,
69                                   struct sk_buff *skb,
70                                   const struct net_device *in,
71                                   const struct net_device *out,
72                                   int (*okfn)(struct sk_buff *))
73 {
74         return arpt_do_table(skb, hook, in, out,
75                              dev_net(out)->ipv4.arptable_filter);
76 }
77
78 static unsigned int arpt_forward_hook(unsigned int hook,
79                                       struct sk_buff *skb,
80                                       const struct net_device *in,
81                                       const struct net_device *out,
82                                       int (*okfn)(struct sk_buff *))
83 {
84         return arpt_do_table(skb, hook, in, out,
85                              dev_net(in)->ipv4.arptable_filter);
86 }
87
88 static struct nf_hook_ops arpt_ops[] __read_mostly = {
89         {
90                 .hook           = arpt_in_hook,
91                 .owner          = THIS_MODULE,
92                 .pf             = NFPROTO_ARP,
93                 .hooknum        = NF_ARP_IN,
94                 .priority       = NF_IP_PRI_FILTER,
95         },
96         {
97                 .hook           = arpt_out_hook,
98                 .owner          = THIS_MODULE,
99                 .pf             = NFPROTO_ARP,
100                 .hooknum        = NF_ARP_OUT,
101                 .priority       = NF_IP_PRI_FILTER,
102         },
103         {
104                 .hook           = arpt_forward_hook,
105                 .owner          = THIS_MODULE,
106                 .pf             = NFPROTO_ARP,
107                 .hooknum        = NF_ARP_FORWARD,
108                 .priority       = NF_IP_PRI_FILTER,
109         },
110 };
111
112 static int __net_init arptable_filter_net_init(struct net *net)
113 {
114         /* Register table */
115         net->ipv4.arptable_filter =
116                 arpt_register_table(net, &packet_filter, &initial_table.repl);
117         if (IS_ERR(net->ipv4.arptable_filter))
118                 return PTR_ERR(net->ipv4.arptable_filter);
119         return 0;
120 }
121
122 static void __net_exit arptable_filter_net_exit(struct net *net)
123 {
124         arpt_unregister_table(net->ipv4.arptable_filter);
125 }
126
127 static struct pernet_operations arptable_filter_net_ops = {
128         .init = arptable_filter_net_init,
129         .exit = arptable_filter_net_exit,
130 };
131
132 static int __init arptable_filter_init(void)
133 {
134         int ret;
135
136         ret = register_pernet_subsys(&arptable_filter_net_ops);
137         if (ret < 0)
138                 return ret;
139
140         ret = nf_register_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
141         if (ret < 0)
142                 goto cleanup_table;
143         return ret;
144
145 cleanup_table:
146         unregister_pernet_subsys(&arptable_filter_net_ops);
147         return ret;
148 }
149
150 static void __exit arptable_filter_fini(void)
151 {
152         nf_unregister_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
153         unregister_pernet_subsys(&arptable_filter_net_ops);
154 }
155
156 module_init(arptable_filter_init);
157 module_exit(arptable_filter_fini);