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));
138 return ip_nat_setup_info(ct, targinfo, hooknum);
141 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
142 const struct net_device *in,
143 const struct net_device *out,
144 unsigned int hooknum,
145 const void *targinfo,
148 struct ip_conntrack *ct;
149 enum ip_conntrack_info ctinfo;
151 #ifdef CONFIG_IP_NF_NAT_LOCAL
152 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
153 || hooknum == NF_IP_LOCAL_OUT);
155 IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING);
158 ct = ip_conntrack_get(*pskb, &ctinfo);
160 /* Connection must be valid and new. */
161 IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
163 return ip_nat_setup_info(ct, targinfo, hooknum);
166 static int ipt_snat_checkentry(const char *tablename,
167 const struct ipt_entry *e,
169 unsigned int targinfosize,
170 unsigned int hook_mask)
172 struct ip_nat_multi_range *mr = targinfo;
174 /* Must be a valid range */
175 if (targinfosize < sizeof(struct ip_nat_multi_range)) {
176 DEBUGP("SNAT: Target size %u too small\n", targinfosize);
180 if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
181 + (sizeof(struct ip_nat_range)
182 * (mr->rangesize - 1))))) {
183 DEBUGP("SNAT: Target size %u wrong for %u ranges\n",
184 targinfosize, mr->rangesize);
188 /* Only allow these for NAT. */
189 if (strcmp(tablename, "nat") != 0) {
190 DEBUGP("SNAT: wrong table %s\n", tablename);
194 if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
195 DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
201 static int ipt_dnat_checkentry(const char *tablename,
202 const struct ipt_entry *e,
204 unsigned int targinfosize,
205 unsigned int hook_mask)
207 struct ip_nat_multi_range *mr = targinfo;
209 /* Must be a valid range */
210 if (targinfosize < sizeof(struct ip_nat_multi_range)) {
211 DEBUGP("DNAT: Target size %u too small\n", targinfosize);
215 if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
216 + (sizeof(struct ip_nat_range)
217 * (mr->rangesize - 1))))) {
218 DEBUGP("DNAT: Target size %u wrong for %u ranges\n",
219 targinfosize, mr->rangesize);
223 /* Only allow these for NAT. */
224 if (strcmp(tablename, "nat") != 0) {
225 DEBUGP("DNAT: wrong table %s\n", tablename);
229 if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
230 DEBUGP("DNAT: hook mask 0x%x bad\n", hook_mask);
234 #ifndef CONFIG_IP_NF_NAT_LOCAL
235 if (hook_mask & (1 << NF_IP_LOCAL_OUT)) {
236 DEBUGP("DNAT: CONFIG_IP_NF_NAT_LOCAL not enabled\n");
245 alloc_null_binding(struct ip_conntrack *conntrack,
246 struct ip_nat_info *info,
247 unsigned int hooknum)
249 /* Force range to this IP; let proto decide mapping for
250 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
251 Use reply in case it's already been mangled (eg local packet).
254 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
255 ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
256 : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
257 struct ip_nat_multi_range mr
258 = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
260 DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
262 return ip_nat_setup_info(conntrack, &mr, hooknum);
265 int ip_nat_rule_find(struct sk_buff **pskb,
266 unsigned int hooknum,
267 const struct net_device *in,
268 const struct net_device *out,
269 struct ip_conntrack *ct,
270 struct ip_nat_info *info)
274 ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
276 if (ret == NF_ACCEPT) {
277 if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
279 ret = alloc_null_binding(ct, info, hooknum);
284 static struct ipt_target ipt_snat_reg = {
286 .target = ipt_snat_target,
287 .checkentry = ipt_snat_checkentry,
290 static struct ipt_target ipt_dnat_reg = {
292 .target = ipt_dnat_target,
293 .checkentry = ipt_dnat_checkentry,
296 int __init ip_nat_rule_init(void)
300 ret = ipt_register_table(&nat_table);
303 ret = ipt_register_target(&ipt_snat_reg);
305 goto unregister_table;
307 ret = ipt_register_target(&ipt_dnat_reg);
309 goto unregister_snat;
314 ipt_unregister_target(&ipt_snat_reg);
316 ipt_unregister_table(&nat_table);
321 void ip_nat_rule_cleanup(void)
323 ipt_unregister_target(&ipt_dnat_reg);
324 ipt_unregister_target(&ipt_snat_reg);
325 ipt_unregister_table(&nat_table);