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