1de86282d23249c4181f92c80106c1dafcb81f8f
[linux-2.6.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 void *targinfo,
107                                     void *userinfo)
108 {
109         struct ip_conntrack *ct;
110         enum ip_conntrack_info ctinfo;
111         const struct ip_nat_multi_range_compat *mr = targinfo;
112
113         IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
114
115         ct = ip_conntrack_get(*pskb, &ctinfo);
116
117         /* Connection must be valid and new. */
118         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
119                             || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
120         IP_NF_ASSERT(out);
121
122         return ip_nat_setup_info(ct, &mr->range[0], hooknum);
123 }
124
125 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
126 static void warn_if_extra_mangle(u32 dstip, u32 srcip)
127 {
128         static int warned = 0;
129         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
130         struct rtable *rt;
131
132         if (ip_route_output_key(&rt, &fl) != 0)
133                 return;
134
135         if (rt->rt_src != srcip && !warned) {
136                 printk("NAT: no longer support implicit source local NAT\n");
137                 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
138                        NIPQUAD(srcip), NIPQUAD(dstip));
139                 warned = 1;
140         }
141         ip_rt_put(rt);
142 }
143
144 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
145                                     const struct net_device *in,
146                                     const struct net_device *out,
147                                     unsigned int hooknum,
148                                     const void *targinfo,
149                                     void *userinfo)
150 {
151         struct ip_conntrack *ct;
152         enum ip_conntrack_info ctinfo;
153         const struct ip_nat_multi_range_compat *mr = targinfo;
154
155         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
156                      || hooknum == NF_IP_LOCAL_OUT);
157
158         ct = ip_conntrack_get(*pskb, &ctinfo);
159
160         /* Connection must be valid and new. */
161         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
162
163         if (hooknum == NF_IP_LOCAL_OUT
164             && mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
165                 warn_if_extra_mangle((*pskb)->nh.iph->daddr,
166                                      mr->range[0].min_ip);
167
168         return ip_nat_setup_info(ct, &mr->range[0], hooknum);
169 }
170
171 static int ipt_snat_checkentry(const char *tablename,
172                                const void *entry,
173                                void *targinfo,
174                                unsigned int targinfosize,
175                                unsigned int hook_mask)
176 {
177         struct ip_nat_multi_range_compat *mr = targinfo;
178
179         /* Must be a valid range */
180         if (mr->rangesize != 1) {
181                 printk("SNAT: multiple ranges no longer supported\n");
182                 return 0;
183         }
184
185         if (targinfosize != IPT_ALIGN(sizeof(struct ip_nat_multi_range_compat))) {
186                 DEBUGP("SNAT: Target size %u wrong for %u ranges\n",
187                        targinfosize, mr->rangesize);
188                 return 0;
189         }
190
191         /* Only allow these for NAT. */
192         if (strcmp(tablename, "nat") != 0) {
193                 DEBUGP("SNAT: wrong table %s\n", tablename);
194                 return 0;
195         }
196
197         if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
198                 DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
199                 return 0;
200         }
201         return 1;
202 }
203
204 static int ipt_dnat_checkentry(const char *tablename,
205                                const void *entry,
206                                void *targinfo,
207                                unsigned int targinfosize,
208                                unsigned int hook_mask)
209 {
210         struct ip_nat_multi_range_compat *mr = targinfo;
211
212         /* Must be a valid range */
213         if (mr->rangesize != 1) {
214                 printk("DNAT: multiple ranges no longer supported\n");
215                 return 0;
216         }
217
218         if (targinfosize != IPT_ALIGN(sizeof(struct ip_nat_multi_range_compat))) {
219                 DEBUGP("DNAT: Target size %u wrong for %u ranges\n",
220                        targinfosize, mr->rangesize);
221                 return 0;
222         }
223
224         /* Only allow these for NAT. */
225         if (strcmp(tablename, "nat") != 0) {
226                 DEBUGP("DNAT: wrong table %s\n", tablename);
227                 return 0;
228         }
229
230         if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
231                 DEBUGP("DNAT: hook mask 0x%x bad\n", hook_mask);
232                 return 0;
233         }
234         
235         return 1;
236 }
237
238 inline unsigned int
239 alloc_null_binding(struct ip_conntrack *conntrack,
240                    struct ip_nat_info *info,
241                    unsigned int hooknum)
242 {
243         /* Force range to this IP; let proto decide mapping for
244            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
245            Use reply in case it's already been mangled (eg local packet).
246         */
247         u_int32_t ip
248                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
249                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
250                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
251         struct ip_nat_range range
252                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
253
254         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
255                NIPQUAD(ip));
256         return ip_nat_setup_info(conntrack, &range, hooknum);
257 }
258
259 unsigned int
260 alloc_null_binding_confirmed(struct ip_conntrack *conntrack,
261                              struct ip_nat_info *info,
262                              unsigned int hooknum)
263 {
264         u_int32_t ip
265                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
266                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
267                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
268         u_int16_t all
269                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
270                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
271                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
272         struct ip_nat_range range
273                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
274
275         DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
276                conntrack, NIPQUAD(ip));
277         return ip_nat_setup_info(conntrack, &range, hooknum);
278 }
279
280 int ip_nat_rule_find(struct sk_buff **pskb,
281                      unsigned int hooknum,
282                      const struct net_device *in,
283                      const struct net_device *out,
284                      struct ip_conntrack *ct,
285                      struct ip_nat_info *info)
286 {
287         int ret;
288
289         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
290
291         if (ret == NF_ACCEPT) {
292                 if (!ip_nat_initialized(ct, HOOK2MANIP(hooknum)))
293                         /* NUL mapping */
294                         ret = alloc_null_binding(ct, info, hooknum);
295         }
296         return ret;
297 }
298
299 static struct ipt_target ipt_snat_reg = {
300         .name           = "SNAT",
301         .target         = ipt_snat_target,
302         .checkentry     = ipt_snat_checkentry,
303 };
304
305 static struct ipt_target ipt_dnat_reg = {
306         .name           = "DNAT",
307         .target         = ipt_dnat_target,
308         .checkentry     = ipt_dnat_checkentry,
309 };
310
311 int __init ip_nat_rule_init(void)
312 {
313         int ret;
314
315         ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
316         if (ret != 0)
317                 return ret;
318         ret = ipt_register_target(&ipt_snat_reg);
319         if (ret != 0)
320                 goto unregister_table;
321
322         ret = ipt_register_target(&ipt_dnat_reg);
323         if (ret != 0)
324                 goto unregister_snat;
325
326         return ret;
327
328  unregister_snat:
329         ipt_unregister_target(&ipt_snat_reg);
330  unregister_table:
331         ipt_unregister_table(&nat_table);
332
333         return ret;
334 }
335
336 void ip_nat_rule_cleanup(void)
337 {
338         ipt_unregister_target(&ipt_dnat_reg);
339         ipt_unregister_target(&ipt_snat_reg);
340         ipt_unregister_table(&nat_table);
341 }