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 },
51 [NF_IP_PRE_ROUTING
] = 0,
52 [NF_IP_POST_ROUTING
] = sizeof(struct ipt_standard
),
53 [NF_IP_LOCAL_OUT
] = sizeof(struct ipt_standard
) * 2 },
59 .target_offset
= sizeof(struct ipt_entry
),
60 .next_offset
= sizeof(struct ipt_standard
),
65 .target_size
= IPT_ALIGN(sizeof(struct ipt_standard_target
)),
68 .verdict
= -NF_ACCEPT
- 1,
74 .target_offset
= sizeof(struct ipt_entry
),
75 .next_offset
= sizeof(struct ipt_standard
),
80 .target_size
= IPT_ALIGN(sizeof(struct ipt_standard_target
)),
83 .verdict
= -NF_ACCEPT
- 1,
89 .target_offset
= sizeof(struct ipt_entry
),
90 .next_offset
= sizeof(struct ipt_standard
),
95 .target_size
= IPT_ALIGN(sizeof(struct ipt_standard_target
)),
98 .verdict
= -NF_ACCEPT
- 1,
105 .target_offset
= sizeof(struct ipt_entry
),
106 .next_offset
= sizeof(struct ipt_error
),
112 .target_size
= IPT_ALIGN(sizeof(struct ipt_error_target
)),
113 .name
= IPT_ERROR_TARGET
,
117 .errorname
= "ERROR",
122 static struct xt_table nat_table
= {
124 .valid_hooks
= NAT_VALID_HOOKS
,
125 .lock
= RW_LOCK_UNLOCKED
,
131 static unsigned int ipt_snat_target(struct sk_buff
**pskb
,
132 const struct net_device
*in
,
133 const struct net_device
*out
,
134 unsigned int hooknum
,
135 const struct xt_target
*target
,
136 const void *targinfo
)
139 enum ip_conntrack_info ctinfo
;
140 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
142 NF_CT_ASSERT(hooknum
== NF_IP_POST_ROUTING
);
144 ct
= nf_ct_get(*pskb
, &ctinfo
);
146 /* Connection must be valid and new. */
147 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
148 ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
));
151 return nf_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
154 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
155 static void warn_if_extra_mangle(__be32 dstip
, __be32 srcip
)
157 static int warned
= 0;
158 struct flowi fl
= { .nl_u
= { .ip4_u
= { .daddr
= dstip
} } };
161 if (ip_route_output_key(&rt
, &fl
) != 0)
164 if (rt
->rt_src
!= srcip
&& !warned
) {
165 printk("NAT: no longer support implicit source local NAT\n");
166 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
167 NIPQUAD(srcip
), NIPQUAD(dstip
));
173 static unsigned int ipt_dnat_target(struct sk_buff
**pskb
,
174 const struct net_device
*in
,
175 const struct net_device
*out
,
176 unsigned int hooknum
,
177 const struct xt_target
*target
,
178 const void *targinfo
)
181 enum ip_conntrack_info ctinfo
;
182 const struct nf_nat_multi_range_compat
*mr
= targinfo
;
184 NF_CT_ASSERT(hooknum
== NF_IP_PRE_ROUTING
||
185 hooknum
== NF_IP_LOCAL_OUT
);
187 ct
= nf_ct_get(*pskb
, &ctinfo
);
189 /* Connection must be valid and new. */
190 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
192 if (hooknum
== NF_IP_LOCAL_OUT
&&
193 mr
->range
[0].flags
& IP_NAT_RANGE_MAP_IPS
)
194 warn_if_extra_mangle(ip_hdr(*pskb
)->daddr
,
195 mr
->range
[0].min_ip
);
197 return nf_nat_setup_info(ct
, &mr
->range
[0], hooknum
);
200 static int ipt_snat_checkentry(const char *tablename
,
202 const struct xt_target
*target
,
204 unsigned int hook_mask
)
206 struct nf_nat_multi_range_compat
*mr
= targinfo
;
208 /* Must be a valid range */
209 if (mr
->rangesize
!= 1) {
210 printk("SNAT: multiple ranges no longer supported\n");
216 static int ipt_dnat_checkentry(const char *tablename
,
218 const struct xt_target
*target
,
220 unsigned int hook_mask
)
222 struct nf_nat_multi_range_compat
*mr
= targinfo
;
224 /* Must be a valid range */
225 if (mr
->rangesize
!= 1) {
226 printk("DNAT: multiple ranges no longer supported\n");
229 if (mr
->range
[0].flags
& IP_NAT_RANGE_PROTO_RANDOM
) {
230 printk("DNAT: port randomization not supported\n");
237 alloc_null_binding(struct nf_conn
*ct
,
238 struct nf_nat_info
*info
,
239 unsigned int hooknum
)
241 /* Force range to this IP; let proto decide mapping for
242 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
243 Use reply in case it's already been mangled (eg local packet).
246 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
247 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
248 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
);
249 struct nf_nat_range range
250 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { 0 }, { 0 } };
252 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
254 return nf_nat_setup_info(ct
, &range
, hooknum
);
258 alloc_null_binding_confirmed(struct nf_conn
*ct
,
259 struct nf_nat_info
*info
,
260 unsigned int hooknum
)
263 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
264 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
265 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
);
267 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
268 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u
.all
269 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u
.all
);
270 struct nf_nat_range range
271 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { all
}, { all
} };
273 DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
275 return nf_nat_setup_info(ct
, &range
, hooknum
);
278 int nf_nat_rule_find(struct sk_buff
**pskb
,
279 unsigned int hooknum
,
280 const struct net_device
*in
,
281 const struct net_device
*out
,
283 struct nf_nat_info
*info
)
287 ret
= ipt_do_table(pskb
, hooknum
, in
, out
, &nat_table
);
289 if (ret
== NF_ACCEPT
) {
290 if (!nf_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
292 ret
= alloc_null_binding(ct
, info
, hooknum
);
297 static struct xt_target ipt_snat_reg
= {
299 .target
= ipt_snat_target
,
300 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
302 .hooks
= 1 << NF_IP_POST_ROUTING
,
303 .checkentry
= ipt_snat_checkentry
,
307 static struct xt_target ipt_dnat_reg
= {
309 .target
= ipt_dnat_target
,
310 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
312 .hooks
= (1 << NF_IP_PRE_ROUTING
) | (1 << NF_IP_LOCAL_OUT
),
313 .checkentry
= ipt_dnat_checkentry
,
317 int __init
nf_nat_rule_init(void)
321 ret
= ipt_register_table(&nat_table
, &nat_initial_table
.repl
);
324 ret
= xt_register_target(&ipt_snat_reg
);
326 goto unregister_table
;
328 ret
= xt_register_target(&ipt_dnat_reg
);
330 goto unregister_snat
;
335 xt_unregister_target(&ipt_snat_reg
);
337 ipt_unregister_table(&nat_table
);
342 void nf_nat_rule_cleanup(void)
344 xt_unregister_target(&ipt_dnat_reg
);
345 xt_unregister_target(&ipt_snat_reg
);
346 ipt_unregister_table(&nat_table
);