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
,
73 static unsigned int ipt_snat_target(struct sk_buff
*skb
,
74 const struct net_device
*in
,
75 const struct net_device
*out
,
77 const struct xt_target
*target
,
81 enum ip_conntrack_info ctinfo
;
82 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
84 NF_CT_ASSERT(hooknum
== NF_IP_POST_ROUTING
);
86 ct
= nf_ct_get(skb
, &ctinfo
);
88 /* Connection must be valid and new. */
89 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
90 ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
));
93 return nf_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
96 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
97 static void warn_if_extra_mangle(__be32 dstip
, __be32 srcip
)
99 static int warned
= 0;
100 struct flowi fl
= { .nl_u
= { .ip4_u
= { .daddr
= dstip
} } };
103 if (ip_route_output_key(&rt
, &fl
) != 0)
106 if (rt
->rt_src
!= srcip
&& !warned
) {
107 printk("NAT: no longer support implicit source local NAT\n");
108 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
109 NIPQUAD(srcip
), NIPQUAD(dstip
));
115 static unsigned int ipt_dnat_target(struct sk_buff
*skb
,
116 const struct net_device
*in
,
117 const struct net_device
*out
,
118 unsigned int hooknum
,
119 const struct xt_target
*target
,
120 const void *targinfo
)
123 enum ip_conntrack_info ctinfo
;
124 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
126 NF_CT_ASSERT(hooknum
== NF_IP_PRE_ROUTING
||
127 hooknum
== NF_IP_LOCAL_OUT
);
129 ct
= nf_ct_get(skb
, &ctinfo
);
131 /* Connection must be valid and new. */
132 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
134 if (hooknum
== NF_IP_LOCAL_OUT
&&
135 mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
)
136 warn_if_extra_mangle(ip_hdr(skb
)->daddr
,
137 mr
->range
[0].min_ip
);
139 return nf_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
142 static int ipt_snat_checkentry(const char *tablename
,
144 const struct xt_target
*target
,
146 unsigned int hook_mask
)
148 struct nf_nat_multi_range_compat
*mr
= targinfo
;
150 /* Must be a valid range */
151 if (mr
->rangesize
!= 1) {
152 printk("SNAT: multiple ranges no longer supported\n");
158 static int ipt_dnat_checkentry(const char *tablename
,
160 const struct xt_target
*target
,
162 unsigned int hook_mask
)
164 struct nf_nat_multi_range_compat
*mr
= targinfo
;
166 /* Must be a valid range */
167 if (mr
->rangesize
!= 1) {
168 printk("DNAT: multiple ranges no longer supported\n");
175 alloc_null_binding(struct nf_conn
*ct
, unsigned int hooknum
)
177 /* Force range to this IP; let proto decide mapping for
178 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
179 Use reply in case it's already been mangled (eg local packet).
182 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
183 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
184 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
);
185 struct nf_nat_range range
186 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { 0 }, { 0 } };
188 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
190 return nf_nat_setup_info(ct
, &range
, hooknum
);
194 alloc_null_binding_confirmed(struct nf_conn
*ct
, unsigned int hooknum
)
197 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
198 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
199 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
);
201 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
202 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u
.all
203 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u
.all
);
204 struct nf_nat_range range
205 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { all
}, { all
} };
207 DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
209 return nf_nat_setup_info(ct
, &range
, hooknum
);
212 int nf_nat_rule_find(struct sk_buff
*skb
,
213 unsigned int hooknum
,
214 const struct net_device
*in
,
215 const struct net_device
*out
,
220 ret
= ipt_do_table(skb
, hooknum
, in
, out
, &nat_table
);
222 if (ret
== NF_ACCEPT
) {
223 if (!nf_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
225 ret
= alloc_null_binding(ct
, hooknum
);
230 static struct xt_target ipt_snat_reg
= {
232 .target
= ipt_snat_target
,
233 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
235 .hooks
= 1 << NF_IP_POST_ROUTING
,
236 .checkentry
= ipt_snat_checkentry
,
240 static struct xt_target ipt_dnat_reg
= {
242 .target
= ipt_dnat_target
,
243 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
245 .hooks
= (1 << NF_IP_PRE_ROUTING
) | (1 << NF_IP_LOCAL_OUT
),
246 .checkentry
= ipt_dnat_checkentry
,
250 int __init
nf_nat_rule_init(void)
254 ret
= ipt_register_table(&nat_table
, &nat_initial_table
.repl
);
257 ret
= xt_register_target(&ipt_snat_reg
);
259 goto unregister_table
;
261 ret
= xt_register_target(&ipt_dnat_reg
);
263 goto unregister_snat
;
268 xt_unregister_target(&ipt_snat_reg
);
270 ipt_unregister_table(&nat_table
);
275 void nf_nat_rule_cleanup(void)
277 xt_unregister_target(&ipt_dnat_reg
);
278 xt_unregister_target(&ipt_snat_reg
);
279 ipt_unregister_table(&nat_table
);