vserver 1.9.3
[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 <linux/bitops.h>
20
21 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
22 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
23
24 #include <linux/netfilter_ipv4/ip_tables.h>
25 #include <linux/netfilter_ipv4/ip_nat.h>
26 #include <linux/netfilter_ipv4/ip_nat_core.h>
27 #include <linux/netfilter_ipv4/ip_nat_rule.h>
28 #include <linux/netfilter_ipv4/listhelp.h>
29
30 #if 0
31 #define DEBUGP printk
32 #else
33 #define DEBUGP(format, args...)
34 #endif
35
36 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
37
38 /* Standard entry. */
39 struct ipt_standard
40 {
41         struct ipt_entry entry;
42         struct ipt_standard_target target;
43 };
44
45 struct ipt_error_target
46 {
47         struct ipt_entry_target target;
48         char errorname[IPT_FUNCTION_MAXNAMELEN];
49 };
50
51 struct ipt_error
52 {
53         struct ipt_entry entry;
54         struct ipt_error_target target;
55 };
56
57 static struct
58 {
59         struct ipt_replace repl;
60         struct ipt_standard entries[3];
61         struct ipt_error term;
62 } nat_initial_table __initdata
63 = { { "nat", NAT_VALID_HOOKS, 4,
64       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
65       { [NF_IP_PRE_ROUTING] = 0,
66         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
67         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
68       { [NF_IP_PRE_ROUTING] = 0,
69         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
70         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
71       0, NULL, { } },
72     {
73             /* PRE_ROUTING */
74             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
75                 0,
76                 sizeof(struct ipt_entry),
77                 sizeof(struct ipt_standard),
78                 0, { 0, 0 }, { } },
79               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
80                 -NF_ACCEPT - 1 } },
81             /* POST_ROUTING */
82             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
83                 0,
84                 sizeof(struct ipt_entry),
85                 sizeof(struct ipt_standard),
86                 0, { 0, 0 }, { } },
87               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
88                 -NF_ACCEPT - 1 } },
89             /* LOCAL_OUT */
90             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
91                 0,
92                 sizeof(struct ipt_entry),
93                 sizeof(struct ipt_standard),
94                 0, { 0, 0 }, { } },
95               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
96                 -NF_ACCEPT - 1 } }
97     },
98     /* ERROR */
99     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
100         0,
101         sizeof(struct ipt_entry),
102         sizeof(struct ipt_error),
103         0, { 0, 0 }, { } },
104       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
105           { } },
106         "ERROR"
107       }
108     }
109 };
110
111 static struct ipt_table nat_table = {
112         .name           = "nat",
113         .table          = &nat_initial_table.repl,
114         .valid_hooks    = NAT_VALID_HOOKS,
115         .lock           = RW_LOCK_UNLOCKED,
116         .me             = THIS_MODULE,
117 };
118
119 /* Source NAT */
120 static unsigned int ipt_snat_target(struct sk_buff **pskb,
121                                     const struct net_device *in,
122                                     const struct net_device *out,
123                                     unsigned int hooknum,
124                                     const void *targinfo,
125                                     void *userinfo)
126 {
127         struct ip_conntrack *ct;
128         enum ip_conntrack_info ctinfo;
129
130         IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
131
132         ct = ip_conntrack_get(*pskb, &ctinfo);
133
134         /* Connection must be valid and new. */
135         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
136                             || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
137         IP_NF_ASSERT(out);
138
139         return ip_nat_setup_info(ct, targinfo, hooknum);
140 }
141
142 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
143                                     const struct net_device *in,
144                                     const struct net_device *out,
145                                     unsigned int hooknum,
146                                     const void *targinfo,
147                                     void *userinfo)
148 {
149         struct ip_conntrack *ct;
150         enum ip_conntrack_info ctinfo;
151
152 #ifdef CONFIG_IP_NF_NAT_LOCAL
153         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
154                      || hooknum == NF_IP_LOCAL_OUT);
155 #else
156         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING);
157 #endif
158
159         ct = ip_conntrack_get(*pskb, &ctinfo);
160
161         /* Connection must be valid and new. */
162         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
163
164         return ip_nat_setup_info(ct, targinfo, hooknum);
165 }
166
167 static int ipt_snat_checkentry(const char *tablename,
168                                const struct ipt_entry *e,
169                                void *targinfo,
170                                unsigned int targinfosize,
171                                unsigned int hook_mask)
172 {
173         struct ip_nat_multi_range *mr = targinfo;
174
175         /* Must be a valid range */
176         if (targinfosize < sizeof(struct ip_nat_multi_range)) {
177                 DEBUGP("SNAT: Target size %u too small\n", targinfosize);
178                 return 0;
179         }
180
181         if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
182                                        + (sizeof(struct ip_nat_range)
183                                           * (mr->rangesize - 1))))) {
184                 DEBUGP("SNAT: Target size %u wrong for %u ranges\n",
185                        targinfosize, mr->rangesize);
186                 return 0;
187         }
188
189         /* Only allow these for NAT. */
190         if (strcmp(tablename, "nat") != 0) {
191                 DEBUGP("SNAT: wrong table %s\n", tablename);
192                 return 0;
193         }
194
195         if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
196                 DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
197                 return 0;
198         }
199         return 1;
200 }
201
202 static int ipt_dnat_checkentry(const char *tablename,
203                                const struct ipt_entry *e,
204                                void *targinfo,
205                                unsigned int targinfosize,
206                                unsigned int hook_mask)
207 {
208         struct ip_nat_multi_range *mr = targinfo;
209
210         /* Must be a valid range */
211         if (targinfosize < sizeof(struct ip_nat_multi_range)) {
212                 DEBUGP("DNAT: Target size %u too small\n", targinfosize);
213                 return 0;
214         }
215
216         if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
217                                        + (sizeof(struct ip_nat_range)
218                                           * (mr->rangesize - 1))))) {
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 #ifndef CONFIG_IP_NF_NAT_LOCAL
236         if (hook_mask & (1 << NF_IP_LOCAL_OUT)) {
237                 DEBUGP("DNAT: CONFIG_IP_NF_NAT_LOCAL not enabled\n");
238                 return 0;
239         }
240 #endif
241
242         return 1;
243 }
244
245 inline unsigned int
246 alloc_null_binding(struct ip_conntrack *conntrack,
247                    struct ip_nat_info *info,
248                    unsigned int hooknum)
249 {
250         /* Force range to this IP; let proto decide mapping for
251            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
252            Use reply in case it's already been mangled (eg local packet).
253         */
254         u_int32_t ip
255                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
256                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
257                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
258         struct ip_nat_multi_range mr
259                 = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
260
261         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
262                NIPQUAD(ip));
263         return ip_nat_setup_info(conntrack, &mr, hooknum);
264 }
265
266 int ip_nat_rule_find(struct sk_buff **pskb,
267                      unsigned int hooknum,
268                      const struct net_device *in,
269                      const struct net_device *out,
270                      struct ip_conntrack *ct,
271                      struct ip_nat_info *info)
272 {
273         int ret;
274
275         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
276
277         if (ret == NF_ACCEPT) {
278                 if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
279                         /* NUL mapping */
280                         ret = alloc_null_binding(ct, info, hooknum);
281         }
282         return ret;
283 }
284
285 static struct ipt_target ipt_snat_reg = {
286         .name           = "SNAT",
287         .target         = ipt_snat_target,
288         .checkentry     = ipt_snat_checkentry,
289 };
290
291 static struct ipt_target ipt_dnat_reg = {
292         .name           = "DNAT",
293         .target         = ipt_dnat_target,
294         .checkentry     = ipt_dnat_checkentry,
295 };
296
297 int __init ip_nat_rule_init(void)
298 {
299         int ret;
300
301         ret = ipt_register_table(&nat_table);
302         if (ret != 0)
303                 return ret;
304         ret = ipt_register_target(&ipt_snat_reg);
305         if (ret != 0)
306                 goto unregister_table;
307
308         ret = ipt_register_target(&ipt_dnat_reg);
309         if (ret != 0)
310                 goto unregister_snat;
311
312         return ret;
313
314  unregister_snat:
315         ipt_unregister_target(&ipt_snat_reg);
316  unregister_table:
317         ipt_unregister_table(&nat_table);
318
319         return ret;
320 }
321
322 void ip_nat_rule_cleanup(void)
323 {
324         ipt_unregister_target(&ipt_dnat_reg);
325         ipt_unregister_target(&ipt_snat_reg);
326         ipt_unregister_table(&nat_table);
327 }