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>
27 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
31 struct ipt_replace repl;
32 struct ipt_standard entries[3];
33 struct ipt_error term;
34 } nat_initial_table __initdata = {
37 .valid_hooks = NAT_VALID_HOOKS,
39 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
41 [NF_IP_PRE_ROUTING] = 0,
42 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
43 [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 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
53 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
54 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
56 .term = IPT_ERROR_INIT, /* ERROR */
59 static struct xt_table nat_table = {
61 .valid_hooks = NAT_VALID_HOOKS,
62 .lock = RW_LOCK_UNLOCKED,
68 static unsigned int ipt_snat_target(struct sk_buff **pskb,
69 const struct net_device *in,
70 const struct net_device *out,
72 const struct xt_target *target,
76 enum ip_conntrack_info ctinfo;
77 const struct nf_nat_multi_range_compat *mr = targinfo;
79 NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING);
81 ct = nf_ct_get(*pskb, &ctinfo);
83 /* Connection must be valid and new. */
84 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
85 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
88 return nf_nat_setup_info(ct, &mr->range[0], hooknum);
91 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
92 static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
94 static int warned = 0;
95 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
98 if (ip_route_output_key(&rt, &fl) != 0)
101 if (rt->rt_src != srcip && !warned) {
102 printk("NAT: no longer support implicit source local NAT\n");
103 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
104 NIPQUAD(srcip), NIPQUAD(dstip));
110 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
111 const struct net_device *in,
112 const struct net_device *out,
113 unsigned int hooknum,
114 const struct xt_target *target,
115 const void *targinfo)
118 enum ip_conntrack_info ctinfo;
119 const struct nf_nat_multi_range_compat *mr = targinfo;
121 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
122 hooknum == NF_IP_LOCAL_OUT);
124 ct = nf_ct_get(*pskb, &ctinfo);
126 /* Connection must be valid and new. */
127 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
129 if (hooknum == NF_IP_LOCAL_OUT &&
130 mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
131 warn_if_extra_mangle(ip_hdr(*pskb)->daddr,
132 mr->range[0].min_ip);
134 return nf_nat_setup_info(ct, &mr->range[0], hooknum);
137 static bool ipt_snat_checkentry(const char *tablename,
139 const struct xt_target *target,
141 unsigned int hook_mask)
143 struct nf_nat_multi_range_compat *mr = targinfo;
145 /* Must be a valid range */
146 if (mr->rangesize != 1) {
147 printk("SNAT: multiple ranges no longer supported\n");
153 static bool ipt_dnat_checkentry(const char *tablename,
155 const struct xt_target *target,
157 unsigned int hook_mask)
159 struct nf_nat_multi_range_compat *mr = targinfo;
161 /* Must be a valid range */
162 if (mr->rangesize != 1) {
163 printk("DNAT: multiple ranges no longer supported\n");
170 alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
172 /* Force range to this IP; let proto decide mapping for
173 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
174 Use reply in case it's already been mangled (eg local packet).
177 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
178 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
179 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
180 struct nf_nat_range range
181 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
183 pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
185 return nf_nat_setup_info(ct, &range, hooknum);
189 alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum)
192 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
193 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
194 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
196 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
197 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
198 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
199 struct nf_nat_range range
200 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
202 pr_debug("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
204 return nf_nat_setup_info(ct, &range, hooknum);
207 int nf_nat_rule_find(struct sk_buff **pskb,
208 unsigned int hooknum,
209 const struct net_device *in,
210 const struct net_device *out,
215 ret = ipt_do_table(pskb, hooknum, in, out, &nat_table);
217 if (ret == NF_ACCEPT) {
218 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
220 ret = alloc_null_binding(ct, hooknum);
225 static struct xt_target ipt_snat_reg __read_mostly = {
227 .target = ipt_snat_target,
228 .targetsize = sizeof(struct nf_nat_multi_range_compat),
230 .hooks = 1 << NF_IP_POST_ROUTING,
231 .checkentry = ipt_snat_checkentry,
235 static struct xt_target ipt_dnat_reg __read_mostly = {
237 .target = ipt_dnat_target,
238 .targetsize = sizeof(struct nf_nat_multi_range_compat),
240 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
241 .checkentry = ipt_dnat_checkentry,
245 int __init nf_nat_rule_init(void)
249 ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
252 ret = xt_register_target(&ipt_snat_reg);
254 goto unregister_table;
256 ret = xt_register_target(&ipt_dnat_reg);
258 goto unregister_snat;
263 xt_unregister_target(&ipt_snat_reg);
265 ipt_unregister_table(&nat_table);
270 void nf_nat_rule_cleanup(void)
272 xt_unregister_target(&ipt_dnat_reg);
273 xt_unregister_target(&ipt_snat_reg);
274 ipt_unregister_table(&nat_table);