This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / ipv4 / netfilter / nf_nat_rule.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2006 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 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_nat_rule.h>
26
27 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
32
33 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
34
35 static struct
36 {
37         struct ipt_replace repl;
38         struct ipt_standard entries[3];
39         struct ipt_error term;
40 } nat_initial_table __initdata = {
41         .repl = {
42                 .name = "nat",
43                 .valid_hooks = NAT_VALID_HOOKS,
44                 .num_entries = 4,
45                 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
46                 .hook_entry = {
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                 .underflow = {
51                         [NF_IP_PRE_ROUTING] = 0,
52                         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
53                         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
54         },
55         .entries = {
56                 /* PRE_ROUTING */
57                 {
58                         .entry = {
59                                 .target_offset = sizeof(struct ipt_entry),
60                                 .next_offset = sizeof(struct ipt_standard),
61                         },
62                         .target = {
63                                 .target = {
64                                         .u = {
65                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
66                                         },
67                                 },
68                                 .verdict = -NF_ACCEPT - 1,
69                         },
70                 },
71                 /* POST_ROUTING */
72                 {
73                         .entry = {
74                                 .target_offset = sizeof(struct ipt_entry),
75                                 .next_offset = sizeof(struct ipt_standard),
76                         },
77                         .target = {
78                                 .target = {
79                                         .u = {
80                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
81                                         },
82                                 },
83                                 .verdict = -NF_ACCEPT - 1,
84                         },
85                 },
86                 /* LOCAL_OUT */
87                 {
88                         .entry = {
89                                 .target_offset = sizeof(struct ipt_entry),
90                                 .next_offset = sizeof(struct ipt_standard),
91                         },
92                         .target = {
93                                 .target = {
94                                         .u = {
95                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
96                                         },
97                                 },
98                                 .verdict = -NF_ACCEPT - 1,
99                         },
100                 },
101         },
102         /* ERROR */
103         .term = {
104                 .entry = {
105                         .target_offset = sizeof(struct ipt_entry),
106                         .next_offset = sizeof(struct ipt_error),
107                 },
108                 .target = {
109                         .target = {
110                                 .u = {
111                                         .user = {
112                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_error_target)),
113                                                 .name = IPT_ERROR_TARGET,
114                                         },
115                                 },
116                         },
117                         .errorname = "ERROR",
118                 },
119         }
120 };
121
122 static struct ipt_table nat_table = {
123         .name           = "nat",
124         .valid_hooks    = NAT_VALID_HOOKS,
125         .lock           = RW_LOCK_UNLOCKED,
126         .me             = THIS_MODULE,
127         .af             = AF_INET,
128 };
129
130 /* Source NAT */
131 static unsigned int ipt_snat_target(struct sk_buff **pskb,
132                                     const struct net_device *in,
133                                     const struct net_device *out,
134                                     unsigned int hooknum,
135                                     const struct xt_target *target,
136                                     const void *targinfo)
137 {
138         struct nf_conn *ct;
139         enum ip_conntrack_info ctinfo;
140         const struct nf_nat_multi_range_compat *mr = targinfo;
141
142         NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING);
143
144         ct = nf_ct_get(*pskb, &ctinfo);
145
146         /* Connection must be valid and new. */
147         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
148                             ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
149         NF_CT_ASSERT(out);
150
151         return nf_nat_setup_info(ct, &mr->range[0], hooknum);
152 }
153
154 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
155 static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
156 {
157         static int warned = 0;
158         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
159         struct rtable *rt;
160
161         if (ip_route_output_key(&rt, &fl) != 0)
162                 return;
163
164         if (rt->rt_src != srcip && !warned) {
165                 printk("NAT: no longer support implicit source local NAT\n");
166                 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
167                        NIPQUAD(srcip), NIPQUAD(dstip));
168                 warned = 1;
169         }
170         ip_rt_put(rt);
171 }
172
173 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
174                                     const struct net_device *in,
175                                     const struct net_device *out,
176                                     unsigned int hooknum,
177                                     const struct xt_target *target,
178                                     const void *targinfo)
179 {
180         struct nf_conn *ct;
181         enum ip_conntrack_info ctinfo;
182         const struct nf_nat_multi_range_compat *mr = targinfo;
183
184         NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
185                      hooknum == NF_IP_LOCAL_OUT);
186
187         ct = nf_ct_get(*pskb, &ctinfo);
188
189         /* Connection must be valid and new. */
190         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
191
192         if (hooknum == NF_IP_LOCAL_OUT &&
193             mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
194                 warn_if_extra_mangle((*pskb)->nh.iph->daddr,
195                                      mr->range[0].min_ip);
196
197         return nf_nat_setup_info(ct, &mr->range[0], hooknum);
198 }
199
200 static int ipt_snat_checkentry(const char *tablename,
201                                const void *entry,
202                                const struct xt_target *target,
203                                void *targinfo,
204                                unsigned int hook_mask)
205 {
206         struct nf_nat_multi_range_compat *mr = targinfo;
207
208         /* Must be a valid range */
209         if (mr->rangesize != 1) {
210                 printk("SNAT: multiple ranges no longer supported\n");
211                 return 0;
212         }
213         return 1;
214 }
215
216 static int ipt_dnat_checkentry(const char *tablename,
217                                const void *entry,
218                                const struct xt_target *target,
219                                void *targinfo,
220                                unsigned int hook_mask)
221 {
222         struct nf_nat_multi_range_compat *mr = targinfo;
223
224         /* Must be a valid range */
225         if (mr->rangesize != 1) {
226                 printk("DNAT: multiple ranges no longer supported\n");
227                 return 0;
228         }
229         return 1;
230 }
231
232 inline unsigned int
233 alloc_null_binding(struct nf_conn *ct,
234                    struct nf_nat_info *info,
235                    unsigned int hooknum)
236 {
237         /* Force range to this IP; let proto decide mapping for
238            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
239            Use reply in case it's already been mangled (eg local packet).
240         */
241         __be32 ip
242                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
243                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
244                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
245         struct nf_nat_range range
246                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
247
248         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
249                ct, NIPQUAD(ip));
250         return nf_nat_setup_info(ct, &range, hooknum);
251 }
252
253 unsigned int
254 alloc_null_binding_confirmed(struct nf_conn *ct,
255                              struct nf_nat_info *info,
256                              unsigned int hooknum)
257 {
258         __be32 ip
259                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
260                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
261                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
262         u_int16_t all
263                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
264                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
265                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
266         struct nf_nat_range range
267                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
268
269         DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
270                ct, NIPQUAD(ip));
271         return nf_nat_setup_info(ct, &range, hooknum);
272 }
273
274 int nf_nat_rule_find(struct sk_buff **pskb,
275                      unsigned int hooknum,
276                      const struct net_device *in,
277                      const struct net_device *out,
278                      struct nf_conn *ct,
279                      struct nf_nat_info *info)
280 {
281         int ret;
282
283         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table);
284
285         if (ret == NF_ACCEPT) {
286                 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
287                         /* NUL mapping */
288                         ret = alloc_null_binding(ct, info, hooknum);
289         }
290         return ret;
291 }
292
293 static struct ipt_target ipt_snat_reg = {
294         .name           = "SNAT",
295         .target         = ipt_snat_target,
296         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
297         .table          = "nat",
298         .hooks          = 1 << NF_IP_POST_ROUTING,
299         .checkentry     = ipt_snat_checkentry,
300         .family         = AF_INET,
301 };
302
303 static struct xt_target ipt_dnat_reg = {
304         .name           = "DNAT",
305         .target         = ipt_dnat_target,
306         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
307         .table          = "nat",
308         .hooks          = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
309         .checkentry     = ipt_dnat_checkentry,
310         .family         = AF_INET,
311 };
312
313 int __init nf_nat_rule_init(void)
314 {
315         int ret;
316
317         ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
318         if (ret != 0)
319                 return ret;
320         ret = xt_register_target(&ipt_snat_reg);
321         if (ret != 0)
322                 goto unregister_table;
323
324         ret = xt_register_target(&ipt_dnat_reg);
325         if (ret != 0)
326                 goto unregister_snat;
327
328         return ret;
329
330  unregister_snat:
331         xt_unregister_target(&ipt_snat_reg);
332  unregister_table:
333         ipt_unregister_table(&nat_table);
334
335         return ret;
336 }
337
338 void nf_nat_rule_cleanup(void)
339 {
340         xt_unregister_target(&ipt_dnat_reg);
341         xt_unregister_target(&ipt_snat_reg);
342         ipt_unregister_table(&nat_table);
343 }