1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 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 <net/netfilter/nf_nat.h>
24 #include <net/netfilter/nf_nat_core.h>
25 #include <net/netfilter/nf_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 = {
43 .valid_hooks = NAT_VALID_HOOKS,
45 .size = 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
52 [NF_IP_PRE_ROUTING] = 0,
53 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
54 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
58 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
59 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
60 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
62 .term = IPT_ERROR_INIT, /* ERROR */
65 static struct xt_table nat_table = {
67 .valid_hooks = NAT_VALID_HOOKS,
68 .lock = RW_LOCK_UNLOCKED,
74 static unsigned int ipt_snat_target(struct sk_buff **pskb,
75 const struct net_device *in,
76 const struct net_device *out,
78 const struct xt_target *target,
82 enum ip_conntrack_info ctinfo;
83 const struct nf_nat_multi_range_compat *mr = targinfo;
85 NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING);
87 ct = nf_ct_get(*pskb, &ctinfo);
89 /* Connection must be valid and new. */
90 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
91 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
94 return nf_nat_setup_info(ct, &mr->range[0], hooknum);
97 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
98 static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
100 static int warned = 0;
101 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
104 if (ip_route_output_key(&rt, &fl) != 0)
107 if (rt->rt_src != srcip && !warned) {
108 printk("NAT: no longer support implicit source local NAT\n");
109 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
110 NIPQUAD(srcip), NIPQUAD(dstip));
116 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
117 const struct net_device *in,
118 const struct net_device *out,
119 unsigned int hooknum,
120 const struct xt_target *target,
121 const void *targinfo)
124 enum ip_conntrack_info ctinfo;
125 const struct nf_nat_multi_range_compat *mr = targinfo;
127 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
128 hooknum == NF_IP_LOCAL_OUT);
130 ct = nf_ct_get(*pskb, &ctinfo);
132 /* Connection must be valid and new. */
133 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
135 if (hooknum == NF_IP_LOCAL_OUT &&
136 mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
137 warn_if_extra_mangle(ip_hdr(*pskb)->daddr,
138 mr->range[0].min_ip);
140 return nf_nat_setup_info(ct, &mr->range[0], hooknum);
143 static bool ipt_snat_checkentry(const char *tablename,
145 const struct xt_target *target,
147 unsigned int hook_mask)
149 struct nf_nat_multi_range_compat *mr = targinfo;
151 /* Must be a valid range */
152 if (mr->rangesize != 1) {
153 printk("SNAT: multiple ranges no longer supported\n");
159 static bool ipt_dnat_checkentry(const char *tablename,
161 const struct xt_target *target,
163 unsigned int hook_mask)
165 struct nf_nat_multi_range_compat *mr = targinfo;
167 /* Must be a valid range */
168 if (mr->rangesize != 1) {
169 printk("DNAT: multiple ranges no longer supported\n");
176 alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
178 /* Force range to this IP; let proto decide mapping for
179 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
180 Use reply in case it's already been mangled (eg local packet).
183 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
184 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
185 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
186 struct nf_nat_range range
187 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
189 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
191 return nf_nat_setup_info(ct, &range, hooknum);
195 alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum)
198 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
199 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
200 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
202 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
203 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
204 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
205 struct nf_nat_range range
206 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
208 DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
210 return nf_nat_setup_info(ct, &range, hooknum);
213 int nf_nat_rule_find(struct sk_buff **pskb,
214 unsigned int hooknum,
215 const struct net_device *in,
216 const struct net_device *out,
221 ret = ipt_do_table(pskb, hooknum, in, out, &nat_table);
223 if (ret == NF_ACCEPT) {
224 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
226 ret = alloc_null_binding(ct, hooknum);
231 static struct xt_target ipt_snat_reg __read_mostly = {
233 .target = ipt_snat_target,
234 .targetsize = sizeof(struct nf_nat_multi_range_compat),
236 .hooks = 1 << NF_IP_POST_ROUTING,
237 .checkentry = ipt_snat_checkentry,
241 static struct xt_target ipt_dnat_reg __read_mostly = {
243 .target = ipt_dnat_target,
244 .targetsize = sizeof(struct nf_nat_multi_range_compat),
246 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
247 .checkentry = ipt_dnat_checkentry,
251 int __init nf_nat_rule_init(void)
255 ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
258 ret = xt_register_target(&ipt_snat_reg);
260 goto unregister_table;
262 ret = xt_register_target(&ipt_dnat_reg);
264 goto unregister_snat;
269 xt_unregister_target(&ipt_snat_reg);
271 ipt_unregister_table(&nat_table);
276 void nf_nat_rule_cleanup(void)
278 xt_unregister_target(&ipt_dnat_reg);
279 xt_unregister_target(&ipt_snat_reg);
280 ipt_unregister_table(&nat_table);