netfilter: xtables: use xt_table for hook instantiation
[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 const 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 const struct xt_table packet_filter = {
49         .name           = "filter",
50         .valid_hooks    = FILTER_VALID_HOOKS,
51         .me             = THIS_MODULE,
52         .af             = NFPROTO_ARP,
53         .priority       = NF_IP_PRI_FILTER,
54 };
55
56 /* The work comes in here from netfilter.c */
57 static unsigned int
58 arptable_filter_hook(unsigned int hook, struct sk_buff *skb,
59                      const struct net_device *in, const struct net_device *out,
60                      int (*okfn)(struct sk_buff *))
61 {
62         const struct net *net = dev_net((in != NULL) ? in : out);
63
64         return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter);
65 }
66
67 static struct nf_hook_ops *arpfilter_ops __read_mostly;
68
69 static int __net_init arptable_filter_net_init(struct net *net)
70 {
71         /* Register table */
72         net->ipv4.arptable_filter =
73                 arpt_register_table(net, &packet_filter, &initial_table.repl);
74         if (IS_ERR(net->ipv4.arptable_filter))
75                 return PTR_ERR(net->ipv4.arptable_filter);
76         return 0;
77 }
78
79 static void __net_exit arptable_filter_net_exit(struct net *net)
80 {
81         arpt_unregister_table(net->ipv4.arptable_filter);
82 }
83
84 static struct pernet_operations arptable_filter_net_ops = {
85         .init = arptable_filter_net_init,
86         .exit = arptable_filter_net_exit,
87 };
88
89 static int __init arptable_filter_init(void)
90 {
91         int ret;
92
93         ret = register_pernet_subsys(&arptable_filter_net_ops);
94         if (ret < 0)
95                 return ret;
96
97         arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook);
98         if (IS_ERR(arpfilter_ops)) {
99                 ret = PTR_ERR(arpfilter_ops);
100                 goto cleanup_table;
101         }
102         return ret;
103
104 cleanup_table:
105         unregister_pernet_subsys(&arptable_filter_net_ops);
106         return ret;
107 }
108
109 static void __exit arptable_filter_fini(void)
110 {
111         xt_hook_unlink(&packet_filter, arpfilter_ops);
112         unregister_pernet_subsys(&arptable_filter_net_ops);
113 }
114
115 module_init(arptable_filter_init);
116 module_exit(arptable_filter_fini);