1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
9 /* Everything about the rules for NAT. */
10 #include <linux/types.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>
22 #define ASSERT_READ_LOCK(x)
23 #define ASSERT_WRITE_LOCK(x)
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>
34 #define DEBUGP(format, args...)
37 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
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 },
56 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
58 sizeof(struct ipt_entry
),
59 sizeof(struct ipt_standard
),
61 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
64 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
66 sizeof(struct ipt_entry
),
67 sizeof(struct ipt_standard
),
69 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
72 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
74 sizeof(struct ipt_entry
),
75 sizeof(struct ipt_standard
),
77 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
81 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
83 sizeof(struct ipt_entry
),
84 sizeof(struct ipt_error
),
86 { { { { IPT_ALIGN(sizeof(struct ipt_error_target
)), IPT_ERROR_TARGET
} },
93 static struct ipt_table nat_table
= {
95 .valid_hooks
= NAT_VALID_HOOKS
,
96 .lock
= RW_LOCK_UNLOCKED
,
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
)
109 struct ip_conntrack
*ct
;
110 enum ip_conntrack_info ctinfo
;
111 const struct ip_nat_multi_range_compat
*mr
= targinfo
;
113 IP_NF_ASSERT(hooknum
== NF_IP_POST_ROUTING
);
115 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
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
));
122 return ip_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
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
)
128 static int warned
= 0;
129 struct flowi fl
= { .nl_u
= { .ip4_u
= { .daddr
= dstip
} } };
132 if (ip_route_output_key(&rt
, &fl
) != 0)
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
));
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 struct ipt_target
*target
,
149 const void *targinfo
)
151 struct ip_conntrack
*ct
;
152 enum ip_conntrack_info ctinfo
;
153 const struct ip_nat_multi_range_compat
*mr
= targinfo
;
155 IP_NF_ASSERT(hooknum
== NF_IP_PRE_ROUTING
156 || hooknum
== NF_IP_LOCAL_OUT
);
158 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
160 /* Connection must be valid and new. */
161 IP_NF_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
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
);
168 return ip_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
171 static int ipt_snat_checkentry(const char *tablename
,
173 const struct ipt_target
*target
,
175 unsigned int targinfosize
,
176 unsigned int hook_mask
)
178 struct ip_nat_multi_range_compat
*mr
= targinfo
;
180 /* Must be a valid range */
181 if (mr
->rangesize
!= 1) {
182 printk("SNAT: multiple ranges no longer supported\n");
188 static int ipt_dnat_checkentry(const char *tablename
,
190 const struct ipt_target
*target
,
192 unsigned int targinfosize
,
193 unsigned int hook_mask
)
195 struct ip_nat_multi_range_compat
*mr
= targinfo
;
197 /* Must be a valid range */
198 if (mr
->rangesize
!= 1) {
199 printk("DNAT: multiple ranges no longer supported\n");
206 alloc_null_binding(struct ip_conntrack
*conntrack
,
207 struct ip_nat_info
*info
,
208 unsigned int hooknum
)
210 /* Force range to this IP; let proto decide mapping for
211 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
212 Use reply in case it's already been mangled (eg local packet).
215 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
216 ? conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.ip
217 : conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.ip
);
218 struct ip_nat_range range
219 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { 0 }, { 0 } };
221 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack
,
223 return ip_nat_setup_info(conntrack
, &range
, hooknum
);
227 alloc_null_binding_confirmed(struct ip_conntrack
*conntrack
,
228 struct ip_nat_info
*info
,
229 unsigned int hooknum
)
232 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
233 ? conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.ip
234 : conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.ip
);
236 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
237 ? conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u
.all
238 : conntrack
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u
.all
);
239 struct ip_nat_range range
240 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { all
}, { all
} };
242 DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
243 conntrack
, NIPQUAD(ip
));
244 return ip_nat_setup_info(conntrack
, &range
, hooknum
);
247 int ip_nat_rule_find(struct sk_buff
**pskb
,
248 unsigned int hooknum
,
249 const struct net_device
*in
,
250 const struct net_device
*out
,
251 struct ip_conntrack
*ct
,
252 struct ip_nat_info
*info
)
256 ret
= ipt_do_table(pskb
, hooknum
, in
, out
, &nat_table
);
258 if (ret
== NF_ACCEPT
) {
259 if (!ip_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
261 ret
= alloc_null_binding(ct
, info
, hooknum
);
266 static struct ipt_target ipt_snat_reg
= {
268 .target
= ipt_snat_target
,
269 .targetsize
= sizeof(struct ip_nat_multi_range_compat
),
271 .hooks
= 1 << NF_IP_POST_ROUTING
,
272 .checkentry
= ipt_snat_checkentry
,
275 static struct ipt_target ipt_dnat_reg
= {
277 .target
= ipt_dnat_target
,
278 .targetsize
= sizeof(struct ip_nat_multi_range_compat
),
280 .hooks
= (1 << NF_IP_PRE_ROUTING
) | (1 << NF_IP_LOCAL_OUT
),
281 .checkentry
= ipt_dnat_checkentry
,
284 int __init
ip_nat_rule_init(void)
288 ret
= ipt_register_table(&nat_table
, &nat_initial_table
.repl
);
291 ret
= ipt_register_target(&ipt_snat_reg
);
293 goto unregister_table
;
295 ret
= ipt_register_target(&ipt_dnat_reg
);
297 goto unregister_snat
;
302 ipt_unregister_target(&ipt_snat_reg
);
304 ipt_unregister_table(&nat_table
);
309 void ip_nat_rule_cleanup(void)
311 ipt_unregister_target(&ipt_dnat_reg
);
312 ipt_unregister_target(&ipt_snat_reg
);
313 ipt_unregister_table(&nat_table
);