ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / iptable_mangle.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  * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
12  */
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netdevice.h>
17 #include <linux/skbuff.h>
18 #include <net/sock.h>
19 #include <net/route.h>
20 #include <linux/ip.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
24 MODULE_DESCRIPTION("iptables mangle table");
25
26 #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \
27                             (1 << NF_IP_LOCAL_IN) | \
28                             (1 << NF_IP_FORWARD) | \
29                             (1 << NF_IP_LOCAL_OUT) | \
30                             (1 << NF_IP_POST_ROUTING))
31
32 /* Standard entry. */
33 struct ipt_standard
34 {
35         struct ipt_entry entry;
36         struct ipt_standard_target target;
37 };
38
39 struct ipt_error_target
40 {
41         struct ipt_entry_target target;
42         char errorname[IPT_FUNCTION_MAXNAMELEN];
43 };
44
45 struct ipt_error
46 {
47         struct ipt_entry entry;
48         struct ipt_error_target target;
49 };
50
51 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
52 static struct
53 {
54         struct ipt_replace repl;
55         struct ipt_standard entries[5];
56         struct ipt_error term;
57 } initial_table __initdata
58 = { { "mangle", MANGLE_VALID_HOOKS, 6,
59       sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
60       { [NF_IP_PRE_ROUTING]     = 0,
61         [NF_IP_LOCAL_IN]        = sizeof(struct ipt_standard),
62         [NF_IP_FORWARD]         = sizeof(struct ipt_standard) * 2,
63         [NF_IP_LOCAL_OUT]       = sizeof(struct ipt_standard) * 3,
64         [NF_IP_POST_ROUTING]    = sizeof(struct ipt_standard) * 4 },
65       { [NF_IP_PRE_ROUTING]     = 0,
66         [NF_IP_LOCAL_IN]        = sizeof(struct ipt_standard),
67         [NF_IP_FORWARD]         = sizeof(struct ipt_standard) * 2,
68         [NF_IP_LOCAL_OUT]       = sizeof(struct ipt_standard) * 3,
69         [NF_IP_POST_ROUTING]    = sizeof(struct ipt_standard) * 4 },
70       0, NULL, { } },
71     {
72             /* PRE_ROUTING */
73             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
74                 0,
75                 sizeof(struct ipt_entry),
76                 sizeof(struct ipt_standard),
77                 0, { 0, 0 }, { } },
78               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
79                 -NF_ACCEPT - 1 } },
80             /* LOCAL_IN */
81             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
82                 0,
83                 sizeof(struct ipt_entry),
84                 sizeof(struct ipt_standard),
85                 0, { 0, 0 }, { } },
86               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
87                 -NF_ACCEPT - 1 } },
88             /* FORWARD */
89             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
90                 0,
91                 sizeof(struct ipt_entry),
92                 sizeof(struct ipt_standard),
93                 0, { 0, 0 }, { } },
94               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
95                 -NF_ACCEPT - 1 } },
96             /* LOCAL_OUT */
97             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
98                 0,
99                 sizeof(struct ipt_entry),
100                 sizeof(struct ipt_standard),
101                 0, { 0, 0 }, { } },
102               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
103                 -NF_ACCEPT - 1 } },
104             /* POST_ROUTING */
105             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
106                 0,
107                 sizeof(struct ipt_entry),
108                 sizeof(struct ipt_standard),
109                 0, { 0, 0 }, { } },
110               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
111                 -NF_ACCEPT - 1 } },
112     },
113     /* ERROR */
114     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
115         0,
116         sizeof(struct ipt_entry),
117         sizeof(struct ipt_error),
118         0, { 0, 0 }, { } },
119       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
120           { } },
121         "ERROR"
122       }
123     }
124 };
125
126 static struct ipt_table packet_mangler = {
127         .name           = "mangle",
128         .table          = &initial_table.repl,
129         .valid_hooks    = MANGLE_VALID_HOOKS,
130         .lock           = RW_LOCK_UNLOCKED,
131         .me             = THIS_MODULE,
132 };
133
134 /* The work comes in here from netfilter.c. */
135 static unsigned int
136 ipt_route_hook(unsigned int hook,
137          struct sk_buff **pskb,
138          const struct net_device *in,
139          const struct net_device *out,
140          int (*okfn)(struct sk_buff *))
141 {
142         return ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
143 }
144
145 static unsigned int
146 ipt_local_hook(unsigned int hook,
147                    struct sk_buff **pskb,
148                    const struct net_device *in,
149                    const struct net_device *out,
150                    int (*okfn)(struct sk_buff *))
151 {
152         unsigned int ret;
153         u_int8_t tos;
154         u_int32_t saddr, daddr;
155         unsigned long nfmark;
156
157         /* root is playing with raw sockets. */
158         if ((*pskb)->len < sizeof(struct iphdr)
159             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
160                 if (net_ratelimit())
161                         printk("ipt_hook: happy cracking.\n");
162                 return NF_ACCEPT;
163         }
164
165         /* Save things which could affect route */
166         nfmark = (*pskb)->nfmark;
167         saddr = (*pskb)->nh.iph->saddr;
168         daddr = (*pskb)->nh.iph->daddr;
169         tos = (*pskb)->nh.iph->tos;
170
171         ret = ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
172         /* Reroute for ANY change. */
173         if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
174             && ((*pskb)->nh.iph->saddr != saddr
175                 || (*pskb)->nh.iph->daddr != daddr
176                 || (*pskb)->nfmark != nfmark
177                 || (*pskb)->nh.iph->tos != tos))
178                 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
179
180         return ret;
181 }
182
183 static struct nf_hook_ops ipt_ops[] = {
184         {
185                 .hook           = ipt_route_hook,
186                 .owner          = THIS_MODULE,
187                 .pf             = PF_INET,
188                 .hooknum        = NF_IP_PRE_ROUTING, 
189                 .priority       = NF_IP_PRI_MANGLE,
190         },
191         {
192                 .hook           = ipt_route_hook,
193                 .owner          = THIS_MODULE,
194                 .pf             = PF_INET,
195                 .hooknum        = NF_IP_LOCAL_IN,
196                 .priority       = NF_IP_PRI_MANGLE,
197         },
198         {
199                 .hook           = ipt_route_hook,
200                 .owner          = THIS_MODULE,
201                 .pf             = PF_INET,
202                 .hooknum        = NF_IP_FORWARD,
203                 .priority       = NF_IP_PRI_MANGLE,
204         },
205         {
206                 .hook           = ipt_local_hook,
207                 .owner          = THIS_MODULE,
208                 .pf             = PF_INET,
209                 .hooknum        = NF_IP_LOCAL_OUT,
210                 .priority       = NF_IP_PRI_MANGLE,
211         },
212         {
213                 .hook           = ipt_route_hook,
214                 .owner          = THIS_MODULE,
215                 .pf             = PF_INET,
216                 .hooknum        = NF_IP_POST_ROUTING,
217                 .priority       = NF_IP_PRI_MANGLE,
218         },
219 };
220
221 static int __init init(void)
222 {
223         int ret;
224
225         /* Register table */
226         ret = ipt_register_table(&packet_mangler);
227         if (ret < 0)
228                 return ret;
229
230         /* Register hooks */
231         ret = nf_register_hook(&ipt_ops[0]);
232         if (ret < 0)
233                 goto cleanup_table;
234
235         ret = nf_register_hook(&ipt_ops[1]);
236         if (ret < 0)
237                 goto cleanup_hook0;
238
239         ret = nf_register_hook(&ipt_ops[2]);
240         if (ret < 0)
241                 goto cleanup_hook1;
242
243         ret = nf_register_hook(&ipt_ops[3]);
244         if (ret < 0)
245                 goto cleanup_hook2;
246
247         ret = nf_register_hook(&ipt_ops[4]);
248         if (ret < 0)
249                 goto cleanup_hook3;
250
251         return ret;
252
253  cleanup_hook3:
254         nf_unregister_hook(&ipt_ops[3]);
255  cleanup_hook2:
256         nf_unregister_hook(&ipt_ops[2]);
257  cleanup_hook1:
258         nf_unregister_hook(&ipt_ops[1]);
259  cleanup_hook0:
260         nf_unregister_hook(&ipt_ops[0]);
261  cleanup_table:
262         ipt_unregister_table(&packet_mangler);
263
264         return ret;
265 }
266
267 static void __exit fini(void)
268 {
269         unsigned int i;
270
271         for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
272                 nf_unregister_hook(&ipt_ops[i]);
273
274         ipt_unregister_table(&packet_mangler);
275 }
276
277 module_init(init);
278 module_exit(fini);