Merge branch 'for_paulus' of master.kernel.org:/pub/scm/linux/kernel/git/galak/powerpc
[pandora-kernel.git] / net / ipv4 / netfilter / ip_nat_rule.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8
9 /* Everything about the rules for NAT. */
10 #include <linux/types.h>
11 #include <linux/ip.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter_ipv4.h>
14 #include <linux/module.h>
15 #include <linux/kmod.h>
16 #include <linux/skbuff.h>
17 #include <linux/proc_fs.h>
18 #include <net/checksum.h>
19 #include <net/route.h>
20 #include <linux/bitops.h>
21
22 #define ASSERT_READ_LOCK(x)
23 #define ASSERT_WRITE_LOCK(x)
24
25 #include <linux/netfilter_ipv4/ip_tables.h>
26 #include <linux/netfilter_ipv4/ip_nat.h>
27 #include <linux/netfilter_ipv4/ip_nat_core.h>
28 #include <linux/netfilter_ipv4/ip_nat_rule.h>
29 #include <linux/netfilter_ipv4/listhelp.h>
30
31 #if 0
32 #define DEBUGP printk
33 #else
34 #define DEBUGP(format, args...)
35 #endif
36
37 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
38
39 static struct
40 {
41         struct ipt_replace repl;
42         struct ipt_standard entries[3];
43         struct ipt_error term;
44 } nat_initial_table __initdata
45 = { { "nat", NAT_VALID_HOOKS, 4,
46       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
47       { [NF_IP_PRE_ROUTING] = 0,
48         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
49         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
50       { [NF_IP_PRE_ROUTING] = 0,
51         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
52         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
53       0, NULL, { } },
54     {
55             /* PRE_ROUTING */
56             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
57                 0,
58                 sizeof(struct ipt_entry),
59                 sizeof(struct ipt_standard),
60                 0, { 0, 0 }, { } },
61               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
62                 -NF_ACCEPT - 1 } },
63             /* POST_ROUTING */
64             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
65                 0,
66                 sizeof(struct ipt_entry),
67                 sizeof(struct ipt_standard),
68                 0, { 0, 0 }, { } },
69               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
70                 -NF_ACCEPT - 1 } },
71             /* LOCAL_OUT */
72             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
73                 0,
74                 sizeof(struct ipt_entry),
75                 sizeof(struct ipt_standard),
76                 0, { 0, 0 }, { } },
77               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
78                 -NF_ACCEPT - 1 } }
79     },
80     /* ERROR */
81     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
82         0,
83         sizeof(struct ipt_entry),
84         sizeof(struct ipt_error),
85         0, { 0, 0 }, { } },
86       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
87           { } },
88         "ERROR"
89       }
90     }
91 };
92
93 static struct ipt_table nat_table = {
94         .name           = "nat",
95         .valid_hooks    = NAT_VALID_HOOKS,
96         .lock           = RW_LOCK_UNLOCKED,
97         .me             = THIS_MODULE,
98         .af             = AF_INET,
99 };
100
101 /* Source NAT */
102 static unsigned int ipt_snat_target(struct sk_buff **pskb,
103                                     const struct net_device *in,
104                                     const struct net_device *out,
105                                     unsigned int hooknum,
106                                     const struct ipt_target *target,
107                                     const void *targinfo,
108                                     void *userinfo)
109 {
110         struct ip_conntrack *ct;
111         enum ip_conntrack_info ctinfo;
112         const struct ip_nat_multi_range_compat *mr = targinfo;
113
114         IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
115
116         ct = ip_conntrack_get(*pskb, &ctinfo);
117
118         /* Connection must be valid and new. */
119         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
120                             || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
121         IP_NF_ASSERT(out);
122
123         return ip_nat_setup_info(ct, &mr->range[0], hooknum);
124 }
125
126 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
127 static void warn_if_extra_mangle(u32 dstip, u32 srcip)
128 {
129         static int warned = 0;
130         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
131         struct rtable *rt;
132
133         if (ip_route_output_key(&rt, &fl) != 0)
134                 return;
135
136         if (rt->rt_src != srcip && !warned) {
137                 printk("NAT: no longer support implicit source local NAT\n");
138                 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
139                        NIPQUAD(srcip), NIPQUAD(dstip));
140                 warned = 1;
141         }
142         ip_rt_put(rt);
143 }
144
145 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
146                                     const struct net_device *in,
147                                     const struct net_device *out,
148                                     unsigned int hooknum,
149                                     const struct ipt_target *target,
150                                     const void *targinfo,
151                                     void *userinfo)
152 {
153         struct ip_conntrack *ct;
154         enum ip_conntrack_info ctinfo;
155         const struct ip_nat_multi_range_compat *mr = targinfo;
156
157         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
158                      || hooknum == NF_IP_LOCAL_OUT);
159
160         ct = ip_conntrack_get(*pskb, &ctinfo);
161
162         /* Connection must be valid and new. */
163         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
164
165         if (hooknum == NF_IP_LOCAL_OUT
166             && mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
167                 warn_if_extra_mangle((*pskb)->nh.iph->daddr,
168                                      mr->range[0].min_ip);
169
170         return ip_nat_setup_info(ct, &mr->range[0], hooknum);
171 }
172
173 static int ipt_snat_checkentry(const char *tablename,
174                                const void *entry,
175                                const struct ipt_target *target,
176                                void *targinfo,
177                                unsigned int targinfosize,
178                                unsigned int hook_mask)
179 {
180         struct ip_nat_multi_range_compat *mr = targinfo;
181
182         /* Must be a valid range */
183         if (mr->rangesize != 1) {
184                 printk("SNAT: multiple ranges no longer supported\n");
185                 return 0;
186         }
187         return 1;
188 }
189
190 static int ipt_dnat_checkentry(const char *tablename,
191                                const void *entry,
192                                const struct ipt_target *target,
193                                void *targinfo,
194                                unsigned int targinfosize,
195                                unsigned int hook_mask)
196 {
197         struct ip_nat_multi_range_compat *mr = targinfo;
198
199         /* Must be a valid range */
200         if (mr->rangesize != 1) {
201                 printk("DNAT: multiple ranges no longer supported\n");
202                 return 0;
203         }
204         return 1;
205 }
206
207 inline unsigned int
208 alloc_null_binding(struct ip_conntrack *conntrack,
209                    struct ip_nat_info *info,
210                    unsigned int hooknum)
211 {
212         /* Force range to this IP; let proto decide mapping for
213            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
214            Use reply in case it's already been mangled (eg local packet).
215         */
216         u_int32_t ip
217                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
218                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
219                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
220         struct ip_nat_range range
221                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
222
223         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
224                NIPQUAD(ip));
225         return ip_nat_setup_info(conntrack, &range, hooknum);
226 }
227
228 unsigned int
229 alloc_null_binding_confirmed(struct ip_conntrack *conntrack,
230                              struct ip_nat_info *info,
231                              unsigned int hooknum)
232 {
233         u_int32_t ip
234                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
235                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
236                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
237         u_int16_t all
238                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
239                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
240                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
241         struct ip_nat_range range
242                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
243
244         DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
245                conntrack, NIPQUAD(ip));
246         return ip_nat_setup_info(conntrack, &range, hooknum);
247 }
248
249 int ip_nat_rule_find(struct sk_buff **pskb,
250                      unsigned int hooknum,
251                      const struct net_device *in,
252                      const struct net_device *out,
253                      struct ip_conntrack *ct,
254                      struct ip_nat_info *info)
255 {
256         int ret;
257
258         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
259
260         if (ret == NF_ACCEPT) {
261                 if (!ip_nat_initialized(ct, HOOK2MANIP(hooknum)))
262                         /* NUL mapping */
263                         ret = alloc_null_binding(ct, info, hooknum);
264         }
265         return ret;
266 }
267
268 static struct ipt_target ipt_snat_reg = {
269         .name           = "SNAT",
270         .target         = ipt_snat_target,
271         .targetsize     = sizeof(struct ip_nat_multi_range_compat),
272         .table          = "nat",
273         .hooks          = 1 << NF_IP_POST_ROUTING,
274         .checkentry     = ipt_snat_checkentry,
275 };
276
277 static struct ipt_target ipt_dnat_reg = {
278         .name           = "DNAT",
279         .target         = ipt_dnat_target,
280         .targetsize     = sizeof(struct ip_nat_multi_range_compat),
281         .table          = "nat",
282         .hooks          = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
283         .checkentry     = ipt_dnat_checkentry,
284 };
285
286 int __init ip_nat_rule_init(void)
287 {
288         int ret;
289
290         ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
291         if (ret != 0)
292                 return ret;
293         ret = ipt_register_target(&ipt_snat_reg);
294         if (ret != 0)
295                 goto unregister_table;
296
297         ret = ipt_register_target(&ipt_dnat_reg);
298         if (ret != 0)
299                 goto unregister_snat;
300
301         return ret;
302
303  unregister_snat:
304         ipt_unregister_target(&ipt_snat_reg);
305  unregister_table:
306         ipt_unregister_table(&nat_table);
307
308         return ret;
309 }
310
311 void ip_nat_rule_cleanup(void)
312 {
313         ipt_unregister_target(&ipt_dnat_reg);
314         ipt_unregister_target(&ipt_snat_reg);
315         ipt_unregister_table(&nat_table);
316 }