ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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/netfilter_ipv6/ip6_tables.h>
14
15 MODULE_LICENSE("GPL");
16 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
17 MODULE_DESCRIPTION("ip6tables filter table");
18
19 #define FILTER_VALID_HOOKS ((1 << NF_IP6_LOCAL_IN) | (1 << NF_IP6_FORWARD) | (1 << NF_IP6_LOCAL_OUT))
20
21 /* Standard entry. */
22 struct ip6t_standard
23 {
24         struct ip6t_entry entry;
25         struct ip6t_standard_target target;
26 };
27
28 struct ip6t_error_target
29 {
30         struct ip6t_entry_target target;
31         char errorname[IP6T_FUNCTION_MAXNAMELEN];
32 };
33
34 struct ip6t_error
35 {
36         struct ip6t_entry entry;
37         struct ip6t_error_target target;
38 };
39
40 static struct
41 {
42         struct ip6t_replace repl;
43         struct ip6t_standard entries[3];
44         struct ip6t_error term;
45 } initial_table __initdata
46 = { { "filter", FILTER_VALID_HOOKS, 4,
47       sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error),
48       { [NF_IP6_LOCAL_IN] = 0,
49         [NF_IP6_FORWARD] = sizeof(struct ip6t_standard),
50         [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 },
51       { [NF_IP6_LOCAL_IN] = 0,
52         [NF_IP6_FORWARD] = sizeof(struct ip6t_standard),
53         [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 },
54       0, NULL, { } },
55     {
56             /* LOCAL_IN */
57             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
58                 0,
59                 sizeof(struct ip6t_entry),
60                 sizeof(struct ip6t_standard),
61                 0, { 0, 0 }, { } },
62               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
63                 -NF_ACCEPT - 1 } },
64             /* FORWARD */
65             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
66                 0,
67                 sizeof(struct ip6t_entry),
68                 sizeof(struct ip6t_standard),
69                 0, { 0, 0 }, { } },
70               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
71                 -NF_ACCEPT - 1 } },
72             /* LOCAL_OUT */
73             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
74                 0,
75                 sizeof(struct ip6t_entry),
76                 sizeof(struct ip6t_standard),
77                 0, { 0, 0 }, { } },
78               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
79                 -NF_ACCEPT - 1 } }
80     },
81     /* ERROR */
82     { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
83         0,
84         sizeof(struct ip6t_entry),
85         sizeof(struct ip6t_error),
86         0, { 0, 0 }, { } },
87       { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target)), IP6T_ERROR_TARGET } },
88           { } },
89         "ERROR"
90       }
91     }
92 };
93
94 static struct ip6t_table packet_filter = {
95         .name           = "filter",
96         .table          = &initial_table.repl,
97         .valid_hooks    = FILTER_VALID_HOOKS,
98         .lock           = RW_LOCK_UNLOCKED,
99         .me             = THIS_MODULE,
100 };
101
102 /* The work comes in here from netfilter.c. */
103 static unsigned int
104 ip6t_hook(unsigned int hook,
105          struct sk_buff **pskb,
106          const struct net_device *in,
107          const struct net_device *out,
108          int (*okfn)(struct sk_buff *))
109 {
110         return ip6t_do_table(pskb, hook, in, out, &packet_filter, NULL);
111 }
112
113 static unsigned int
114 ip6t_local_out_hook(unsigned int hook,
115                    struct sk_buff **pskb,
116                    const struct net_device *in,
117                    const struct net_device *out,
118                    int (*okfn)(struct sk_buff *))
119 {
120 #if 0
121         /* root is playing with raw sockets. */
122         if ((*pskb)->len < sizeof(struct iphdr)
123             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
124                 if (net_ratelimit())
125                         printk("ip6t_hook: happy cracking.\n");
126                 return NF_ACCEPT;
127         }
128 #endif
129
130         return ip6t_do_table(pskb, hook, in, out, &packet_filter, NULL);
131 }
132
133 static struct nf_hook_ops ip6t_ops[] = {
134         {
135                 .hook           = ip6t_hook,
136                 .owner          = THIS_MODULE,
137                 .pf             = PF_INET6,
138                 .hooknum        = NF_IP6_LOCAL_IN,
139                 .priority       = NF_IP6_PRI_FILTER,
140         },
141         {
142                 .hook           = ip6t_hook,
143                 .owner          = THIS_MODULE,
144                 .pf             = PF_INET6,
145                 .hooknum        = NF_IP6_FORWARD,
146                 .priority       = NF_IP6_PRI_FILTER,
147         },
148         {
149                 .hook           = ip6t_local_out_hook,
150                 .owner          = THIS_MODULE,
151                 .pf             = PF_INET6,
152                 .hooknum        = NF_IP6_LOCAL_OUT,
153                 .priority       = NF_IP6_PRI_FILTER,
154         },
155 };
156
157 /* Default to forward because I got too much mail already. */
158 static int forward = NF_ACCEPT;
159 MODULE_PARM(forward, "i");
160
161 static int __init init(void)
162 {
163         int ret;
164
165         if (forward < 0 || forward > NF_MAX_VERDICT) {
166                 printk("iptables forward must be 0 or 1\n");
167                 return -EINVAL;
168         }
169
170         /* Entry 1 is the FORWARD hook */
171         initial_table.entries[1].target.verdict = -forward - 1;
172
173         /* Register table */
174         ret = ip6t_register_table(&packet_filter);
175         if (ret < 0)
176                 return ret;
177
178         /* Register hooks */
179         ret = nf_register_hook(&ip6t_ops[0]);
180         if (ret < 0)
181                 goto cleanup_table;
182
183         ret = nf_register_hook(&ip6t_ops[1]);
184         if (ret < 0)
185                 goto cleanup_hook0;
186
187         ret = nf_register_hook(&ip6t_ops[2]);
188         if (ret < 0)
189                 goto cleanup_hook1;
190
191         return ret;
192
193  cleanup_hook1:
194         nf_unregister_hook(&ip6t_ops[1]);
195  cleanup_hook0:
196         nf_unregister_hook(&ip6t_ops[0]);
197  cleanup_table:
198         ip6t_unregister_table(&packet_filter);
199
200         return ret;
201 }
202
203 static void __exit fini(void)
204 {
205         unsigned int i;
206
207         for (i = 0; i < sizeof(ip6t_ops)/sizeof(struct nf_hook_ops); i++)
208                 nf_unregister_hook(&ip6t_ops[i]);
209
210         ip6t_unregister_table(&packet_filter);
211 }
212
213 module_init(init);
214 module_exit(fini);