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 #include <linux/netfilter_ipv4/ip_tables.h>
23 #include <linux/netfilter_ipv4/ip_nat.h>
24 #include <linux/netfilter_ipv4/ip_nat_core.h>
25 #include <linux/netfilter_ipv4/ip_nat_rule.h>
30 #define DEBUGP(format, args...)
33 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
37 struct ipt_replace repl;
38 struct ipt_standard entries[3];
39 struct ipt_error term;
40 } nat_initial_table __initdata
41 = { { "nat", NAT_VALID_HOOKS, 4,
42 sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
43 { [NF_IP_PRE_ROUTING] = 0,
44 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
45 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
46 { [NF_IP_PRE_ROUTING] = 0,
47 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
48 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
52 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
54 sizeof(struct ipt_entry),
55 sizeof(struct ipt_standard),
57 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
60 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
62 sizeof(struct ipt_entry),
63 sizeof(struct ipt_standard),
65 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
68 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
70 sizeof(struct ipt_entry),
71 sizeof(struct ipt_standard),
73 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
77 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
79 sizeof(struct ipt_entry),
80 sizeof(struct ipt_error),
82 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
89 static struct ipt_table nat_table = {
91 .valid_hooks = NAT_VALID_HOOKS,
92 .lock = RW_LOCK_UNLOCKED,
98 static unsigned int ipt_snat_target(struct sk_buff **pskb,
99 const struct net_device *in,
100 const struct net_device *out,
101 unsigned int hooknum,
102 const struct ipt_target *target,
103 const void *targinfo)
105 struct ip_conntrack *ct;
106 enum ip_conntrack_info ctinfo;
107 const struct ip_nat_multi_range_compat *mr = targinfo;
109 IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
111 ct = ip_conntrack_get(*pskb, &ctinfo);
113 /* Connection must be valid and new. */
114 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
115 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
118 return ip_nat_setup_info(ct, &mr->range[0], hooknum);
121 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
122 static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
124 static int warned = 0;
125 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
128 if (ip_route_output_key(&rt, &fl) != 0)
131 if (rt->rt_src != srcip && !warned) {
132 printk("NAT: no longer support implicit source local NAT\n");
133 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
134 NIPQUAD(srcip), NIPQUAD(dstip));
140 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
141 const struct net_device *in,
142 const struct net_device *out,
143 unsigned int hooknum,
144 const struct ipt_target *target,
145 const void *targinfo)
147 struct ip_conntrack *ct;
148 enum ip_conntrack_info ctinfo;
149 const struct ip_nat_multi_range_compat *mr = targinfo;
151 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
152 || hooknum == NF_IP_LOCAL_OUT);
154 ct = ip_conntrack_get(*pskb, &ctinfo);
156 /* Connection must be valid and new. */
157 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
159 if (hooknum == NF_IP_LOCAL_OUT
160 && mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
161 warn_if_extra_mangle((*pskb)->nh.iph->daddr,
162 mr->range[0].min_ip);
164 return ip_nat_setup_info(ct, &mr->range[0], hooknum);
167 static int ipt_snat_checkentry(const char *tablename,
169 const struct ipt_target *target,
171 unsigned int hook_mask)
173 struct ip_nat_multi_range_compat *mr = targinfo;
175 /* Must be a valid range */
176 if (mr->rangesize != 1) {
177 printk("SNAT: multiple ranges no longer supported\n");
183 static int ipt_dnat_checkentry(const char *tablename,
185 const struct ipt_target *target,
187 unsigned int hook_mask)
189 struct ip_nat_multi_range_compat *mr = targinfo;
191 /* Must be a valid range */
192 if (mr->rangesize != 1) {
193 printk("DNAT: multiple ranges no longer supported\n");
200 alloc_null_binding(struct ip_conntrack *conntrack,
201 struct ip_nat_info *info,
202 unsigned int hooknum)
204 /* Force range to this IP; let proto decide mapping for
205 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
206 Use reply in case it's already been mangled (eg local packet).
209 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
210 ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
211 : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
212 struct ip_nat_range range
213 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
215 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
217 return ip_nat_setup_info(conntrack, &range, hooknum);
221 alloc_null_binding_confirmed(struct ip_conntrack *conntrack,
222 struct ip_nat_info *info,
223 unsigned int hooknum)
226 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
227 ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
228 : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
230 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
231 ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
232 : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
233 struct ip_nat_range range
234 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
236 DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
237 conntrack, NIPQUAD(ip));
238 return ip_nat_setup_info(conntrack, &range, hooknum);
241 int ip_nat_rule_find(struct sk_buff **pskb,
242 unsigned int hooknum,
243 const struct net_device *in,
244 const struct net_device *out,
245 struct ip_conntrack *ct,
246 struct ip_nat_info *info)
250 ret = ipt_do_table(pskb, hooknum, in, out, &nat_table);
252 if (ret == NF_ACCEPT) {
253 if (!ip_nat_initialized(ct, HOOK2MANIP(hooknum)))
255 ret = alloc_null_binding(ct, info, hooknum);
260 static struct ipt_target ipt_snat_reg = {
262 .target = ipt_snat_target,
263 .targetsize = sizeof(struct ip_nat_multi_range_compat),
265 .hooks = 1 << NF_IP_POST_ROUTING,
266 .checkentry = ipt_snat_checkentry,
269 static struct ipt_target ipt_dnat_reg = {
271 .target = ipt_dnat_target,
272 .targetsize = sizeof(struct ip_nat_multi_range_compat),
274 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
275 .checkentry = ipt_dnat_checkentry,
278 int __init ip_nat_rule_init(void)
282 ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
285 ret = ipt_register_target(&ipt_snat_reg);
287 goto unregister_table;
289 ret = ipt_register_target(&ipt_dnat_reg);
291 goto unregister_snat;
296 ipt_unregister_target(&ipt_snat_reg);
298 ipt_unregister_table(&nat_table);
303 void ip_nat_rule_cleanup(void)
305 ipt_unregister_target(&ipt_dnat_reg);
306 ipt_unregister_target(&ipt_snat_reg);
307 ipt_unregister_table(&nat_table);