Merge branch 'master' of /repos/git/net-next-2.6
[deliverable/linux.git] / net / ipv4 / netfilter / nf_nat_rule.c
CommitLineData
5b1158e9
JK
1/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3 *
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 */
8
9/* Everything about the rules for NAT. */
ff67e4e4 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5b1158e9
JK
11#include <linux/types.h>
12#include <linux/ip.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter_ipv4.h>
15#include <linux/module.h>
16#include <linux/kmod.h>
17#include <linux/skbuff.h>
18#include <linux/proc_fs.h>
5a0e3ad6 19#include <linux/slab.h>
5b1158e9
JK
20#include <net/checksum.h>
21#include <net/route.h>
22#include <linux/bitops.h>
23
24#include <linux/netfilter_ipv4/ip_tables.h>
25#include <net/netfilter/nf_nat.h>
26#include <net/netfilter/nf_nat_core.h>
27#include <net/netfilter/nf_nat_rule.h>
28
6e23ae2a
PM
29#define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
30 (1 << NF_INET_POST_ROUTING) | \
31 (1 << NF_INET_LOCAL_OUT))
5b1158e9 32
35aad0ff 33static const struct xt_table nat_table = {
5b1158e9
JK
34 .name = "nat",
35 .valid_hooks = NAT_VALID_HOOKS,
5b1158e9 36 .me = THIS_MODULE,
f88e6a8a 37 .af = NFPROTO_IPV4,
5b1158e9
JK
38};
39
40/* Source NAT */
7eb35586 41static unsigned int
4b560b44 42ipt_snat_target(struct sk_buff *skb, const struct xt_action_param *par)
5b1158e9
JK
43{
44 struct nf_conn *ct;
45 enum ip_conntrack_info ctinfo;
7eb35586 46 const struct nf_nat_multi_range_compat *mr = par->targinfo;
5b1158e9 47
7eb35586 48 NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
5b1158e9 49
3db05fea 50 ct = nf_ct_get(skb, &ctinfo);
5b1158e9
JK
51
52 /* Connection must be valid and new. */
53 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
e905a9ed 54 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
7eb35586 55 NF_CT_ASSERT(par->out != NULL);
5b1158e9 56
cc01dcbd 57 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
5b1158e9
JK
58}
59
7eb35586 60static unsigned int
4b560b44 61ipt_dnat_target(struct sk_buff *skb, const struct xt_action_param *par)
5b1158e9
JK
62{
63 struct nf_conn *ct;
64 enum ip_conntrack_info ctinfo;
7eb35586 65 const struct nf_nat_multi_range_compat *mr = par->targinfo;
5b1158e9 66
7eb35586
JE
67 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
68 par->hooknum == NF_INET_LOCAL_OUT);
5b1158e9 69
3db05fea 70 ct = nf_ct_get(skb, &ctinfo);
5b1158e9
JK
71
72 /* Connection must be valid and new. */
73 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
74
cc01dcbd 75 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST);
5b1158e9
JK
76}
77
135367b8 78static int ipt_snat_checkentry(const struct xt_tgchk_param *par)
5b1158e9 79{
af5d6dc2 80 const struct nf_nat_multi_range_compat *mr = par->targinfo;
5b1158e9
JK
81
82 /* Must be a valid range */
83 if (mr->rangesize != 1) {
ff67e4e4 84 pr_info("SNAT: multiple ranges no longer supported\n");
d6b00a53 85 return -EINVAL;
5b1158e9 86 }
d6b00a53 87 return 0;
5b1158e9
JK
88}
89
135367b8 90static int ipt_dnat_checkentry(const struct xt_tgchk_param *par)
5b1158e9 91{
af5d6dc2 92 const struct nf_nat_multi_range_compat *mr = par->targinfo;
5b1158e9
JK
93
94 /* Must be a valid range */
95 if (mr->rangesize != 1) {
ff67e4e4 96 pr_info("DNAT: multiple ranges no longer supported\n");
d6b00a53 97 return -EINVAL;
5b1158e9 98 }
d6b00a53 99 return 0;
5b1158e9
JK
100}
101
170b197c 102unsigned int
ba4c7cba 103alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
5b1158e9
JK
104{
105 /* Force range to this IP; let proto decide mapping for
106 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
107 Use reply in case it's already been mangled (eg local packet).
108 */
109 __be32 ip
110 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
111 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
112 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
113 struct nf_nat_range range
114 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
115
cffee385 116 pr_debug("Allocating NULL binding for %p (%pI4)\n", ct, &ip);
cc01dcbd 117 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
5b1158e9
JK
118}
119
3db05fea 120int nf_nat_rule_find(struct sk_buff *skb,
5b1158e9
JK
121 unsigned int hooknum,
122 const struct net_device *in,
123 const struct net_device *out,
ba4c7cba 124 struct nf_conn *ct)
5b1158e9 125{
e099a173 126 struct net *net = nf_ct_net(ct);
5b1158e9
JK
127 int ret;
128
e099a173 129 ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table);
5b1158e9
JK
130
131 if (ret == NF_ACCEPT) {
132 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
133 /* NUL mapping */
ba4c7cba 134 ret = alloc_null_binding(ct, hooknum);
5b1158e9
JK
135 }
136 return ret;
137}
138
9f15c530 139static struct xt_target ipt_snat_reg __read_mostly = {
5b1158e9
JK
140 .name = "SNAT",
141 .target = ipt_snat_target,
142 .targetsize = sizeof(struct nf_nat_multi_range_compat),
143 .table = "nat",
6e23ae2a 144 .hooks = 1 << NF_INET_POST_ROUTING,
5b1158e9
JK
145 .checkentry = ipt_snat_checkentry,
146 .family = AF_INET,
147};
148
9f15c530 149static struct xt_target ipt_dnat_reg __read_mostly = {
5b1158e9
JK
150 .name = "DNAT",
151 .target = ipt_dnat_target,
152 .targetsize = sizeof(struct nf_nat_multi_range_compat),
153 .table = "nat",
6e23ae2a 154 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
5b1158e9
JK
155 .checkentry = ipt_dnat_checkentry,
156 .family = AF_INET,
157};
158
e099a173
AD
159static int __net_init nf_nat_rule_net_init(struct net *net)
160{
e3eaa991
JE
161 struct ipt_replace *repl;
162
163 repl = ipt_alloc_initial_table(&nat_table);
164 if (repl == NULL)
165 return -ENOMEM;
166 net->ipv4.nat_table = ipt_register_table(net, &nat_table, repl);
167 kfree(repl);
e099a173
AD
168 if (IS_ERR(net->ipv4.nat_table))
169 return PTR_ERR(net->ipv4.nat_table);
170 return 0;
171}
172
173static void __net_exit nf_nat_rule_net_exit(struct net *net)
174{
f54e9367 175 ipt_unregister_table(net, net->ipv4.nat_table);
e099a173
AD
176}
177
178static struct pernet_operations nf_nat_rule_net_ops = {
179 .init = nf_nat_rule_net_init,
180 .exit = nf_nat_rule_net_exit,
181};
182
5b1158e9
JK
183int __init nf_nat_rule_init(void)
184{
185 int ret;
186
e099a173
AD
187 ret = register_pernet_subsys(&nf_nat_rule_net_ops);
188 if (ret != 0)
189 goto out;
5b1158e9
JK
190 ret = xt_register_target(&ipt_snat_reg);
191 if (ret != 0)
192 goto unregister_table;
193
194 ret = xt_register_target(&ipt_dnat_reg);
195 if (ret != 0)
196 goto unregister_snat;
197
198 return ret;
199
200 unregister_snat:
201 xt_unregister_target(&ipt_snat_reg);
202 unregister_table:
e099a173
AD
203 unregister_pernet_subsys(&nf_nat_rule_net_ops);
204 out:
5b1158e9
JK
205 return ret;
206}
207
208void nf_nat_rule_cleanup(void)
209{
210 xt_unregister_target(&ipt_dnat_reg);
211 xt_unregister_target(&ipt_snat_reg);
e099a173 212 unregister_pernet_subsys(&nf_nat_rule_net_ops);
5b1158e9 213}
This page took 0.393818 seconds and 5 git commands to generate.