added files
[bcm963xx.git] / kernel / linux / 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 #ifdef CONFIG_IP_ROUTE_FWMARK
177                 || (*pskb)->nfmark != nfmark
178 #endif
179                 || (*pskb)->nh.iph->tos != tos))
180                 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
181
182         return ret;
183 }
184
185 static struct nf_hook_ops ipt_ops[] = {
186         {
187                 .hook           = ipt_route_hook,
188                 .owner          = THIS_MODULE,
189                 .pf             = PF_INET,
190                 .hooknum        = NF_IP_PRE_ROUTING, 
191                 .priority       = NF_IP_PRI_MANGLE,
192         },
193         {
194                 .hook           = ipt_route_hook,
195                 .owner          = THIS_MODULE,
196                 .pf             = PF_INET,
197                 .hooknum        = NF_IP_LOCAL_IN,
198                 .priority       = NF_IP_PRI_MANGLE,
199         },
200         {
201                 .hook           = ipt_route_hook,
202                 .owner          = THIS_MODULE,
203                 .pf             = PF_INET,
204                 .hooknum        = NF_IP_FORWARD,
205                 .priority       = NF_IP_PRI_MANGLE,
206         },
207         {
208                 .hook           = ipt_local_hook,
209                 .owner          = THIS_MODULE,
210                 .pf             = PF_INET,
211                 .hooknum        = NF_IP_LOCAL_OUT,
212                 .priority       = NF_IP_PRI_MANGLE,
213         },
214         {
215                 .hook           = ipt_route_hook,
216                 .owner          = THIS_MODULE,
217                 .pf             = PF_INET,
218                 .hooknum        = NF_IP_POST_ROUTING,
219                 .priority       = NF_IP_PRI_MANGLE,
220         },
221 };
222
223 static int __init init(void)
224 {
225         int ret;
226
227         /* Register table */
228         ret = ipt_register_table(&packet_mangler);
229         if (ret < 0)
230                 return ret;
231
232         /* Register hooks */
233         ret = nf_register_hook(&ipt_ops[0]);
234         if (ret < 0)
235                 goto cleanup_table;
236
237         ret = nf_register_hook(&ipt_ops[1]);
238         if (ret < 0)
239                 goto cleanup_hook0;
240
241         ret = nf_register_hook(&ipt_ops[2]);
242         if (ret < 0)
243                 goto cleanup_hook1;
244
245         ret = nf_register_hook(&ipt_ops[3]);
246         if (ret < 0)
247                 goto cleanup_hook2;
248
249         ret = nf_register_hook(&ipt_ops[4]);
250         if (ret < 0)
251                 goto cleanup_hook3;
252
253         return ret;
254
255  cleanup_hook3:
256         nf_unregister_hook(&ipt_ops[3]);
257  cleanup_hook2:
258         nf_unregister_hook(&ipt_ops[2]);
259  cleanup_hook1:
260         nf_unregister_hook(&ipt_ops[1]);
261  cleanup_hook0:
262         nf_unregister_hook(&ipt_ops[0]);
263  cleanup_table:
264         ipt_unregister_table(&packet_mangler);
265
266         return ret;
267 }
268
269 static void __exit fini(void)
270 {
271         unsigned int i;
272
273         for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
274                 nf_unregister_hook(&ipt_ops[i]);
275
276         ipt_unregister_table(&packet_mangler);
277 }
278
279 module_init(init);
280 module_exit(fini);