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