Merge branch 'e1000-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[pandora-kernel.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 #include <linux/module.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <net/sock.h>
16 #include <net/route.h>
17 #include <linux/ip.h>
18 #include <net/ip.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables mangle table");
23
24 #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \
25                             (1 << NF_IP_LOCAL_IN) | \
26                             (1 << NF_IP_FORWARD) | \
27                             (1 << NF_IP_LOCAL_OUT) | \
28                             (1 << NF_IP_POST_ROUTING))
29
30 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
31 static struct
32 {
33         struct ipt_replace repl;
34         struct ipt_standard entries[5];
35         struct ipt_error term;
36 } initial_table __initdata
37 = { { "mangle", MANGLE_VALID_HOOKS, 6,
38       sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
39       { [NF_IP_PRE_ROUTING]     = 0,
40         [NF_IP_LOCAL_IN]        = sizeof(struct ipt_standard),
41         [NF_IP_FORWARD]         = sizeof(struct ipt_standard) * 2,
42         [NF_IP_LOCAL_OUT]       = sizeof(struct ipt_standard) * 3,
43         [NF_IP_POST_ROUTING]    = sizeof(struct ipt_standard) * 4 },
44       { [NF_IP_PRE_ROUTING]     = 0,
45         [NF_IP_LOCAL_IN]        = sizeof(struct ipt_standard),
46         [NF_IP_FORWARD]         = sizeof(struct ipt_standard) * 2,
47         [NF_IP_LOCAL_OUT]       = sizeof(struct ipt_standard) * 3,
48         [NF_IP_POST_ROUTING]    = sizeof(struct ipt_standard) * 4 },
49       0, NULL, { } },
50     {
51             /* PRE_ROUTING */
52             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
53                 0,
54                 sizeof(struct ipt_entry),
55                 sizeof(struct ipt_standard),
56                 0, { 0, 0 }, { } },
57               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
58                 -NF_ACCEPT - 1 } },
59             /* LOCAL_IN */
60             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
61                 0,
62                 sizeof(struct ipt_entry),
63                 sizeof(struct ipt_standard),
64                 0, { 0, 0 }, { } },
65               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
66                 -NF_ACCEPT - 1 } },
67             /* FORWARD */
68             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
69                 0,
70                 sizeof(struct ipt_entry),
71                 sizeof(struct ipt_standard),
72                 0, { 0, 0 }, { } },
73               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
74                 -NF_ACCEPT - 1 } },
75             /* LOCAL_OUT */
76             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
77                 0,
78                 sizeof(struct ipt_entry),
79                 sizeof(struct ipt_standard),
80                 0, { 0, 0 }, { } },
81               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
82                 -NF_ACCEPT - 1 } },
83             /* POST_ROUTING */
84             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
85                 0,
86                 sizeof(struct ipt_entry),
87                 sizeof(struct ipt_standard),
88                 0, { 0, 0 }, { } },
89               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
90                 -NF_ACCEPT - 1 } },
91     },
92     /* ERROR */
93     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
94         0,
95         sizeof(struct ipt_entry),
96         sizeof(struct ipt_error),
97         0, { 0, 0 }, { } },
98       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
99           { } },
100         "ERROR"
101       }
102     }
103 };
104
105 static struct xt_table packet_mangler = {
106         .name           = "mangle",
107         .valid_hooks    = MANGLE_VALID_HOOKS,
108         .lock           = RW_LOCK_UNLOCKED,
109         .me             = THIS_MODULE,
110         .af             = AF_INET,
111 };
112
113 /* The work comes in here from netfilter.c. */
114 static unsigned int
115 ipt_route_hook(unsigned int hook,
116          struct sk_buff **pskb,
117          const struct net_device *in,
118          const struct net_device *out,
119          int (*okfn)(struct sk_buff *))
120 {
121         return ipt_do_table(pskb, hook, in, out, &packet_mangler);
122 }
123
124 static unsigned int
125 ipt_local_hook(unsigned int hook,
126                    struct sk_buff **pskb,
127                    const struct net_device *in,
128                    const struct net_device *out,
129                    int (*okfn)(struct sk_buff *))
130 {
131         unsigned int ret;
132         const struct iphdr *iph;
133         u_int8_t tos;
134         __be32 saddr, daddr;
135         u_int32_t mark;
136
137         /* root is playing with raw sockets. */
138         if ((*pskb)->len < sizeof(struct iphdr)
139             || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
140                 if (net_ratelimit())
141                         printk("ipt_hook: happy cracking.\n");
142                 return NF_ACCEPT;
143         }
144
145         /* Save things which could affect route */
146         mark = (*pskb)->mark;
147         iph = ip_hdr(*pskb);
148         saddr = iph->saddr;
149         daddr = iph->daddr;
150         tos = iph->tos;
151
152         ret = ipt_do_table(pskb, hook, in, out, &packet_mangler);
153         /* Reroute for ANY change. */
154         if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
155                 iph = ip_hdr(*pskb);
156
157                 if (iph->saddr != saddr ||
158                     iph->daddr != daddr ||
159                     (*pskb)->mark != mark ||
160                     iph->tos != tos)
161                         if (ip_route_me_harder(pskb, RTN_UNSPEC))
162                                 ret = NF_DROP;
163         }
164
165         return ret;
166 }
167
168 static struct nf_hook_ops ipt_ops[] = {
169         {
170                 .hook           = ipt_route_hook,
171                 .owner          = THIS_MODULE,
172                 .pf             = PF_INET,
173                 .hooknum        = NF_IP_PRE_ROUTING,
174                 .priority       = NF_IP_PRI_MANGLE,
175         },
176         {
177                 .hook           = ipt_route_hook,
178                 .owner          = THIS_MODULE,
179                 .pf             = PF_INET,
180                 .hooknum        = NF_IP_LOCAL_IN,
181                 .priority       = NF_IP_PRI_MANGLE,
182         },
183         {
184                 .hook           = ipt_route_hook,
185                 .owner          = THIS_MODULE,
186                 .pf             = PF_INET,
187                 .hooknum        = NF_IP_FORWARD,
188                 .priority       = NF_IP_PRI_MANGLE,
189         },
190         {
191                 .hook           = ipt_local_hook,
192                 .owner          = THIS_MODULE,
193                 .pf             = PF_INET,
194                 .hooknum        = NF_IP_LOCAL_OUT,
195                 .priority       = NF_IP_PRI_MANGLE,
196         },
197         {
198                 .hook           = ipt_route_hook,
199                 .owner          = THIS_MODULE,
200                 .pf             = PF_INET,
201                 .hooknum        = NF_IP_POST_ROUTING,
202                 .priority       = NF_IP_PRI_MANGLE,
203         },
204 };
205
206 static int __init iptable_mangle_init(void)
207 {
208         int ret;
209
210         /* Register table */
211         ret = ipt_register_table(&packet_mangler, &initial_table.repl);
212         if (ret < 0)
213                 return ret;
214
215         /* Register hooks */
216         ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
217         if (ret < 0)
218                 goto cleanup_table;
219
220         return ret;
221
222  cleanup_table:
223         ipt_unregister_table(&packet_mangler);
224         return ret;
225 }
226
227 static void __exit iptable_mangle_fini(void)
228 {
229         nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
230         ipt_unregister_table(&packet_mangler);
231 }
232
233 module_init(iptable_mangle_init);
234 module_exit(iptable_mangle_fini);