netfilter: xtables: compact table hook functions (1/2)
[safe/jmp/linux-2.6] / net / ipv6 / netfilter / ip6table_filter.c
1 /*
2  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
3  *
4  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/netfilter_ipv6/ip6_tables.h>
15
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18 MODULE_DESCRIPTION("ip6tables filter table");
19
20 #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
21                             (1 << NF_INET_FORWARD) | \
22                             (1 << NF_INET_LOCAL_OUT))
23
24 static struct
25 {
26         struct ip6t_replace repl;
27         struct ip6t_standard entries[3];
28         struct ip6t_error term;
29 } initial_table __net_initdata = {
30         .repl = {
31                 .name = "filter",
32                 .valid_hooks = FILTER_VALID_HOOKS,
33                 .num_entries = 4,
34                 .size = sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error),
35                 .hook_entry = {
36                         [NF_INET_LOCAL_IN] = 0,
37                         [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
38                         [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
39                 },
40                 .underflow = {
41                         [NF_INET_LOCAL_IN] = 0,
42                         [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
43                         [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
44                 },
45         },
46         .entries = {
47                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* LOCAL_IN */
48                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* FORWARD */
49                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* LOCAL_OUT */
50         },
51         .term = IP6T_ERROR_INIT,                /* ERROR */
52 };
53
54 static const struct xt_table packet_filter = {
55         .name           = "filter",
56         .valid_hooks    = FILTER_VALID_HOOKS,
57         .me             = THIS_MODULE,
58         .af             = NFPROTO_IPV6,
59 };
60
61 /* The work comes in here from netfilter.c. */
62 static unsigned int
63 ip6table_filter_hook(unsigned int hook, struct sk_buff *skb,
64                      const struct net_device *in, const struct net_device *out,
65                      int (*okfn)(struct sk_buff *))
66 {
67         if (hook == NF_INET_LOCAL_OUT)
68                 return ip6t_do_table(skb, hook, in, out,
69                                      dev_net(out)->ipv6.ip6table_filter);
70
71         /* INPUT/FORWARD: */
72         return ip6t_do_table(skb, hook, in, out,
73                              dev_net(in)->ipv6.ip6table_filter);
74 }
75
76 static struct nf_hook_ops ip6t_ops[] __read_mostly = {
77         {
78                 .hook           = ip6table_filter_hook,
79                 .owner          = THIS_MODULE,
80                 .pf             = NFPROTO_IPV6,
81                 .hooknum        = NF_INET_LOCAL_IN,
82                 .priority       = NF_IP6_PRI_FILTER,
83         },
84         {
85                 .hook           = ip6table_filter_hook,
86                 .owner          = THIS_MODULE,
87                 .pf             = NFPROTO_IPV6,
88                 .hooknum        = NF_INET_FORWARD,
89                 .priority       = NF_IP6_PRI_FILTER,
90         },
91         {
92                 .hook           = ip6table_filter_hook,
93                 .owner          = THIS_MODULE,
94                 .pf             = NFPROTO_IPV6,
95                 .hooknum        = NF_INET_LOCAL_OUT,
96                 .priority       = NF_IP6_PRI_FILTER,
97         },
98 };
99
100 /* Default to forward because I got too much mail already. */
101 static int forward = NF_ACCEPT;
102 module_param(forward, bool, 0000);
103
104 static int __net_init ip6table_filter_net_init(struct net *net)
105 {
106         /* Register table */
107         net->ipv6.ip6table_filter =
108                 ip6t_register_table(net, &packet_filter, &initial_table.repl);
109         if (IS_ERR(net->ipv6.ip6table_filter))
110                 return PTR_ERR(net->ipv6.ip6table_filter);
111         return 0;
112 }
113
114 static void __net_exit ip6table_filter_net_exit(struct net *net)
115 {
116         ip6t_unregister_table(net, net->ipv6.ip6table_filter);
117 }
118
119 static struct pernet_operations ip6table_filter_net_ops = {
120         .init = ip6table_filter_net_init,
121         .exit = ip6table_filter_net_exit,
122 };
123
124 static int __init ip6table_filter_init(void)
125 {
126         int ret;
127
128         if (forward < 0 || forward > NF_MAX_VERDICT) {
129                 printk("iptables forward must be 0 or 1\n");
130                 return -EINVAL;
131         }
132
133         /* Entry 1 is the FORWARD hook */
134         initial_table.entries[1].target.verdict = -forward - 1;
135
136         ret = register_pernet_subsys(&ip6table_filter_net_ops);
137         if (ret < 0)
138                 return ret;
139
140         /* Register hooks */
141         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
142         if (ret < 0)
143                 goto cleanup_table;
144
145         return ret;
146
147  cleanup_table:
148         unregister_pernet_subsys(&ip6table_filter_net_ops);
149         return ret;
150 }
151
152 static void __exit ip6table_filter_fini(void)
153 {
154         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
155         unregister_pernet_subsys(&ip6table_filter_net_ops);
156 }
157
158 module_init(ip6table_filter_init);
159 module_exit(ip6table_filter_fini);