vserver 1.9.3
[linux-2.6.git] / net / ipv4 / netfilter / iptable_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
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
19 MODULE_DESCRIPTION("iptables filter table");
20
21 #define FILTER_VALID_HOOKS ((1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT))
22
23 /* Standard entry. */
24 struct ipt_standard
25 {
26         struct ipt_entry entry;
27         struct ipt_standard_target target;
28 };
29
30 struct ipt_error_target
31 {
32         struct ipt_entry_target target;
33         char errorname[IPT_FUNCTION_MAXNAMELEN];
34 };
35
36 struct ipt_error
37 {
38         struct ipt_entry entry;
39         struct ipt_error_target target;
40 };
41
42 static struct
43 {
44         struct ipt_replace repl;
45         struct ipt_standard entries[3];
46         struct ipt_error term;
47 } initial_table __initdata
48 = { { "filter", FILTER_VALID_HOOKS, 4,
49       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
50       { [NF_IP_LOCAL_IN] = 0,
51         [NF_IP_FORWARD] = sizeof(struct ipt_standard),
52         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
53       { [NF_IP_LOCAL_IN] = 0,
54         [NF_IP_FORWARD] = sizeof(struct ipt_standard),
55         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
56       0, NULL, { } },
57     {
58             /* LOCAL_IN */
59             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
60                 0,
61                 sizeof(struct ipt_entry),
62                 sizeof(struct ipt_standard),
63                 0, { 0, 0 }, { } },
64               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
65                 -NF_ACCEPT - 1 } },
66             /* FORWARD */
67             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
68                 0,
69                 sizeof(struct ipt_entry),
70                 sizeof(struct ipt_standard),
71                 0, { 0, 0 }, { } },
72               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
73                 -NF_ACCEPT - 1 } },
74             /* LOCAL_OUT */
75             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
76                 0,
77                 sizeof(struct ipt_entry),
78                 sizeof(struct ipt_standard),
79                 0, { 0, 0 }, { } },
80               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
81                 -NF_ACCEPT - 1 } }
82     },
83     /* ERROR */
84     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
85         0,
86         sizeof(struct ipt_entry),
87         sizeof(struct ipt_error),
88         0, { 0, 0 }, { } },
89       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
90           { } },
91         "ERROR"
92       }
93     }
94 };
95
96 static struct ipt_table packet_filter = {
97         .name           = "filter",
98         .table          = &initial_table.repl,
99         .valid_hooks    = FILTER_VALID_HOOKS,
100         .lock           = RW_LOCK_UNLOCKED,
101         .me             = THIS_MODULE
102 };
103
104 /* The work comes in here from netfilter.c. */
105 static unsigned int
106 ipt_hook(unsigned int hook,
107          struct sk_buff **pskb,
108          const struct net_device *in,
109          const struct net_device *out,
110          int (*okfn)(struct sk_buff *))
111 {
112         return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
113 }
114
115 static unsigned int
116 ipt_local_out_hook(unsigned int hook,
117                    struct sk_buff **pskb,
118                    const struct net_device *in,
119                    const struct net_device *out,
120                    int (*okfn)(struct sk_buff *))
121 {
122         /* root is playing with raw sockets. */
123         if ((*pskb)->len < sizeof(struct iphdr)
124             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
125                 if (net_ratelimit())
126                         printk("ipt_hook: happy cracking.\n");
127                 return NF_ACCEPT;
128         }
129
130         return ipt_do_table(pskb, hook, in, out, &packet_filter, NULL);
131 }
132
133 static struct nf_hook_ops ipt_ops[] = {
134         {
135                 .hook           = ipt_hook,
136                 .owner          = THIS_MODULE,
137                 .pf             = PF_INET,
138                 .hooknum        = NF_IP_LOCAL_IN,
139                 .priority       = NF_IP_PRI_FILTER,
140         },
141         {
142                 .hook           = ipt_hook,
143                 .owner          = THIS_MODULE,
144                 .pf             = PF_INET,
145                 .hooknum        = NF_IP_FORWARD,
146                 .priority       = NF_IP_PRI_FILTER,
147         },
148         {
149                 .hook           = ipt_local_out_hook,
150                 .owner          = THIS_MODULE,
151                 .pf             = PF_INET,
152                 .hooknum        = NF_IP_LOCAL_OUT,
153                 .priority       = NF_IP_PRI_FILTER,
154         },
155 };
156
157 /* Default to forward because I got too much mail already. */
158 static int forward = NF_ACCEPT;
159 module_param(forward, bool, 0000);
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 = ipt_register_table(&packet_filter);
175         if (ret < 0)
176                 return ret;
177
178         /* Register hooks */
179         ret = nf_register_hook(&ipt_ops[0]);
180         if (ret < 0)
181                 goto cleanup_table;
182
183         ret = nf_register_hook(&ipt_ops[1]);
184         if (ret < 0)
185                 goto cleanup_hook0;
186
187         ret = nf_register_hook(&ipt_ops[2]);
188         if (ret < 0)
189                 goto cleanup_hook1;
190
191         return ret;
192
193  cleanup_hook1:
194         nf_unregister_hook(&ipt_ops[1]);
195  cleanup_hook0:
196         nf_unregister_hook(&ipt_ops[0]);
197  cleanup_table:
198         ipt_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(ipt_ops)/sizeof(struct nf_hook_ops); i++)
208                 nf_unregister_hook(&ipt_ops[i]);
209
210         ipt_unregister_table(&packet_filter);
211 }
212
213 module_init(init);
214 module_exit(fini);