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 <linux/bitops.h>
21 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
22 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
24 #include <linux/netfilter_ipv4/ip_tables.h>
25 #include <linux/netfilter_ipv4/ip_nat.h>
26 #include <linux/netfilter_ipv4/ip_nat_core.h>
27 #include <linux/netfilter_ipv4/ip_nat_rule.h>
28 #include <linux/netfilter_ipv4/listhelp.h>
33 #define DEBUGP(format, args...)
36 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
41 struct ipt_entry entry;
42 struct ipt_standard_target target;
45 struct ipt_error_target
47 struct ipt_entry_target target;
48 char errorname[IPT_FUNCTION_MAXNAMELEN];
53 struct ipt_entry entry;
54 struct ipt_error_target target;
59 struct ipt_replace repl;
60 struct ipt_standard entries[3];
61 struct ipt_error term;
62 } nat_initial_table __initdata
63 = { { "nat", NAT_VALID_HOOKS, 4,
64 sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
65 { [NF_IP_PRE_ROUTING] = 0,
66 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
67 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
68 { [NF_IP_PRE_ROUTING] = 0,
69 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
70 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
74 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
76 sizeof(struct ipt_entry),
77 sizeof(struct ipt_standard),
79 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
82 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
84 sizeof(struct ipt_entry),
85 sizeof(struct ipt_standard),
87 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
90 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
92 sizeof(struct ipt_entry),
93 sizeof(struct ipt_standard),
95 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
99 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
101 sizeof(struct ipt_entry),
102 sizeof(struct ipt_error),
104 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
111 static struct ipt_table nat_table = {
113 .table = &nat_initial_table.repl,
114 .valid_hooks = NAT_VALID_HOOKS,
115 .lock = RW_LOCK_UNLOCKED,
120 static unsigned int ipt_snat_target(struct sk_buff **pskb,
121 const struct net_device *in,
122 const struct net_device *out,
123 unsigned int hooknum,
124 const void *targinfo,
127 struct ip_conntrack *ct;
128 enum ip_conntrack_info ctinfo;
130 IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
132 ct = ip_conntrack_get(*pskb, &ctinfo);
134 /* Connection must be valid and new. */
135 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED
136 || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
139 return ip_nat_setup_info(ct, targinfo, hooknum);
142 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
143 const struct net_device *in,
144 const struct net_device *out,
145 unsigned int hooknum,
146 const void *targinfo,
149 struct ip_conntrack *ct;
150 enum ip_conntrack_info ctinfo;
152 #ifdef CONFIG_IP_NF_NAT_LOCAL
153 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
154 || hooknum == NF_IP_LOCAL_OUT);
156 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING);
159 ct = ip_conntrack_get(*pskb, &ctinfo);
161 /* Connection must be valid and new. */
162 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
164 return ip_nat_setup_info(ct, targinfo, hooknum);
167 static int ipt_snat_checkentry(const char *tablename,
168 const struct ipt_entry *e,
170 unsigned int targinfosize,
171 unsigned int hook_mask)
173 struct ip_nat_multi_range *mr = targinfo;
175 /* Must be a valid range */
176 if (targinfosize < sizeof(struct ip_nat_multi_range)) {
177 DEBUGP("SNAT: Target size %u too small\n", targinfosize);
181 if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
182 + (sizeof(struct ip_nat_range)
183 * (mr->rangesize - 1))))) {
184 DEBUGP("SNAT: Target size %u wrong for %u ranges\n",
185 targinfosize, mr->rangesize);
189 /* Only allow these for NAT. */
190 if (strcmp(tablename, "nat") != 0) {
191 DEBUGP("SNAT: wrong table %s\n", tablename);
195 if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
196 DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
202 static int ipt_dnat_checkentry(const char *tablename,
203 const struct ipt_entry *e,
205 unsigned int targinfosize,
206 unsigned int hook_mask)
208 struct ip_nat_multi_range *mr = targinfo;
210 /* Must be a valid range */
211 if (targinfosize < sizeof(struct ip_nat_multi_range)) {
212 DEBUGP("DNAT: Target size %u too small\n", targinfosize);
216 if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
217 + (sizeof(struct ip_nat_range)
218 * (mr->rangesize - 1))))) {
219 DEBUGP("DNAT: Target size %u wrong for %u ranges\n",
220 targinfosize, mr->rangesize);
224 /* Only allow these for NAT. */
225 if (strcmp(tablename, "nat") != 0) {
226 DEBUGP("DNAT: wrong table %s\n", tablename);
230 if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
231 DEBUGP("DNAT: hook mask 0x%x bad\n", hook_mask);
235 #ifndef CONFIG_IP_NF_NAT_LOCAL
236 if (hook_mask & (1 << NF_IP_LOCAL_OUT)) {
237 DEBUGP("DNAT: CONFIG_IP_NF_NAT_LOCAL not enabled\n");
246 alloc_null_binding(struct ip_conntrack *conntrack,
247 struct ip_nat_info *info,
248 unsigned int hooknum)
250 /* Force range to this IP; let proto decide mapping for
251 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
252 Use reply in case it's already been mangled (eg local packet).
255 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
256 ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
257 : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
258 struct ip_nat_multi_range mr
259 = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
261 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
263 return ip_nat_setup_info(conntrack, &mr, hooknum);
266 int ip_nat_rule_find(struct sk_buff **pskb,
267 unsigned int hooknum,
268 const struct net_device *in,
269 const struct net_device *out,
270 struct ip_conntrack *ct,
271 struct ip_nat_info *info)
275 ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
277 if (ret == NF_ACCEPT) {
278 if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
280 ret = alloc_null_binding(ct, info, hooknum);
285 static struct ipt_target ipt_snat_reg = {
287 .target = ipt_snat_target,
288 .checkentry = ipt_snat_checkentry,
291 static struct ipt_target ipt_dnat_reg = {
293 .target = ipt_dnat_target,
294 .checkentry = ipt_dnat_checkentry,
297 int __init ip_nat_rule_init(void)
301 ret = ipt_register_table(&nat_table);
304 ret = ipt_register_target(&ipt_snat_reg);
306 goto unregister_table;
308 ret = ipt_register_target(&ipt_dnat_reg);
310 goto unregister_snat;
315 ipt_unregister_target(&ipt_snat_reg);
317 ipt_unregister_table(&nat_table);
322 void ip_nat_rule_cleanup(void)
324 ipt_unregister_target(&ipt_dnat_reg);
325 ipt_unregister_target(&ipt_snat_reg);
326 ipt_unregister_table(&nat_table);