NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / nf_nat_rule.c
blob998d719feaf20b648981afdac01dd8de00fa53c6
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.
7 */
9 /* Everything about the rules for NAT. */
10 #include <linux/types.h>
11 #include <linux/ip.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 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
33 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
35 static struct
37 struct ipt_replace repl;
38 struct ipt_standard entries[3];
39 struct ipt_error term;
40 } nat_initial_table __initdata = {
41 .repl = {
42 .name = "nat",
43 .valid_hooks = NAT_VALID_HOOKS,
44 .num_entries = 4,
45 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
46 .hook_entry = {
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 .underflow = {
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
57 .entries = {
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 = {
66 .name = "nat",
67 .valid_hooks = NAT_VALID_HOOKS,
68 .me = THIS_MODULE,
69 .af = AF_INET,
72 /* Source NAT */
73 static unsigned int ipt_snat_target(struct sk_buff *skb,
74 const struct net_device *in,
75 const struct net_device *out,
76 unsigned int hooknum,
77 const struct xt_target *target,
78 const void *targinfo)
80 struct nf_conn *ct;
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));
91 NF_CT_ASSERT(out);
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 } } };
101 struct rtable *rt;
103 if (ip_route_output_key(&rt, &fl) != 0)
104 return;
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));
110 warned = 1;
112 ip_rt_put(rt);
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)
122 struct nf_conn *ct;
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,
143 const void *entry,
144 const struct xt_target *target,
145 void *targinfo,
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");
153 return 0;
155 return 1;
158 static int ipt_dnat_checkentry(const char *tablename,
159 const void *entry,
160 const struct xt_target *target,
161 void *targinfo,
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");
169 return 0;
171 return 1;
174 inline unsigned int
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).
181 __be32 ip
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",
189 ct, NIPQUAD(ip));
190 return nf_nat_setup_info(ct, &range, hooknum);
193 unsigned int
194 alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum)
196 __be32 ip
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);
200 u_int16_t all
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",
208 ct, NIPQUAD(ip));
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,
216 struct nf_conn *ct)
218 int ret;
220 ret = ipt_do_table(skb, hooknum, in, out, &nat_table);
222 if (ret == NF_ACCEPT) {
223 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
224 /* NUL mapping */
225 ret = alloc_null_binding(ct, hooknum);
227 return ret;
230 static struct xt_target ipt_snat_reg = {
231 .name = "SNAT",
232 .target = ipt_snat_target,
233 .targetsize = sizeof(struct nf_nat_multi_range_compat),
234 .table = "nat",
235 .hooks = 1 << NF_IP_POST_ROUTING,
236 .checkentry = ipt_snat_checkentry,
237 .family = AF_INET,
240 static struct xt_target ipt_dnat_reg = {
241 .name = "DNAT",
242 .target = ipt_dnat_target,
243 .targetsize = sizeof(struct nf_nat_multi_range_compat),
244 .table = "nat",
245 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
246 .checkentry = ipt_dnat_checkentry,
247 .family = AF_INET,
250 int __init nf_nat_rule_init(void)
252 int ret;
254 ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
255 if (ret != 0)
256 return ret;
257 ret = xt_register_target(&ipt_snat_reg);
258 if (ret != 0)
259 goto unregister_table;
261 ret = xt_register_target(&ipt_dnat_reg);
262 if (ret != 0)
263 goto unregister_snat;
265 return ret;
267 unregister_snat:
268 xt_unregister_target(&ipt_snat_reg);
269 unregister_table:
270 ipt_unregister_table(&nat_table);
272 return ret;
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);