Merge git://git.kernel.org/pub/scm/linux/kernel/git/czankel/xtensa-2.6
[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 struct xt_table packet_filter = {
55         .name           = "filter",
56         .valid_hooks    = FILTER_VALID_HOOKS,
57         .lock           = __RW_LOCK_UNLOCKED(packet_filter.lock),
58         .me             = THIS_MODULE,
59         .af             = AF_INET6,
60 };
61
62 /* The work comes in here from netfilter.c. */
63 static unsigned int
64 ip6t_local_in_hook(unsigned int hook,
65                    struct sk_buff *skb,
66                    const struct net_device *in,
67                    const struct net_device *out,
68                    int (*okfn)(struct sk_buff *))
69 {
70         return ip6t_do_table(skb, hook, in, out,
71                              dev_net(in)->ipv6.ip6table_filter);
72 }
73
74 static unsigned int
75 ip6t_forward_hook(unsigned int hook,
76                   struct sk_buff *skb,
77                   const struct net_device *in,
78                   const struct net_device *out,
79                   int (*okfn)(struct sk_buff *))
80 {
81         return ip6t_do_table(skb, hook, in, out,
82                              dev_net(in)->ipv6.ip6table_filter);
83 }
84
85 static unsigned int
86 ip6t_local_out_hook(unsigned int hook,
87                    struct sk_buff *skb,
88                    const struct net_device *in,
89                    const struct net_device *out,
90                    int (*okfn)(struct sk_buff *))
91 {
92 #if 0
93         /* root is playing with raw sockets. */
94         if (skb->len < sizeof(struct iphdr)
95             || ip_hdrlen(skb) < sizeof(struct iphdr)) {
96                 if (net_ratelimit())
97                         printk("ip6t_hook: happy cracking.\n");
98                 return NF_ACCEPT;
99         }
100 #endif
101
102         return ip6t_do_table(skb, hook, in, out,
103                              dev_net(out)->ipv6.ip6table_filter);
104 }
105
106 static struct nf_hook_ops ip6t_ops[] __read_mostly = {
107         {
108                 .hook           = ip6t_local_in_hook,
109                 .owner          = THIS_MODULE,
110                 .pf             = PF_INET6,
111                 .hooknum        = NF_INET_LOCAL_IN,
112                 .priority       = NF_IP6_PRI_FILTER,
113         },
114         {
115                 .hook           = ip6t_forward_hook,
116                 .owner          = THIS_MODULE,
117                 .pf             = PF_INET6,
118                 .hooknum        = NF_INET_FORWARD,
119                 .priority       = NF_IP6_PRI_FILTER,
120         },
121         {
122                 .hook           = ip6t_local_out_hook,
123                 .owner          = THIS_MODULE,
124                 .pf             = PF_INET6,
125                 .hooknum        = NF_INET_LOCAL_OUT,
126                 .priority       = NF_IP6_PRI_FILTER,
127         },
128 };
129
130 /* Default to forward because I got too much mail already. */
131 static int forward = NF_ACCEPT;
132 module_param(forward, bool, 0000);
133
134 static int __net_init ip6table_filter_net_init(struct net *net)
135 {
136         /* Register table */
137         net->ipv6.ip6table_filter =
138                 ip6t_register_table(net, &packet_filter, &initial_table.repl);
139         if (IS_ERR(net->ipv6.ip6table_filter))
140                 return PTR_ERR(net->ipv6.ip6table_filter);
141         return 0;
142 }
143
144 static void __net_exit ip6table_filter_net_exit(struct net *net)
145 {
146         ip6t_unregister_table(net->ipv6.ip6table_filter);
147 }
148
149 static struct pernet_operations ip6table_filter_net_ops = {
150         .init = ip6table_filter_net_init,
151         .exit = ip6table_filter_net_exit,
152 };
153
154 static int __init ip6table_filter_init(void)
155 {
156         int ret;
157
158         if (forward < 0 || forward > NF_MAX_VERDICT) {
159                 printk("iptables forward must be 0 or 1\n");
160                 return -EINVAL;
161         }
162
163         /* Entry 1 is the FORWARD hook */
164         initial_table.entries[1].target.verdict = -forward - 1;
165
166         ret = register_pernet_subsys(&ip6table_filter_net_ops);
167         if (ret < 0)
168                 return ret;
169
170         /* Register hooks */
171         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
172         if (ret < 0)
173                 goto cleanup_table;
174
175         return ret;
176
177  cleanup_table:
178         unregister_pernet_subsys(&ip6table_filter_net_ops);
179         return ret;
180 }
181
182 static void __exit ip6table_filter_fini(void)
183 {
184         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
185         unregister_pernet_subsys(&ip6table_filter_net_ops);
186 }
187
188 module_init(ip6table_filter_init);
189 module_exit(ip6table_filter_fini);