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.
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 <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 #define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
28 (1 << NF_INET_POST_ROUTING) | \
29 (1 << NF_INET_LOCAL_OUT))
33 struct ipt_replace repl
;
34 struct ipt_standard entries
[3];
35 struct ipt_error term
;
36 } nat_initial_table __net_initdata
= {
39 .valid_hooks
= NAT_VALID_HOOKS
,
41 .size
= sizeof(struct ipt_standard
) * 3 + sizeof(struct ipt_error
),
43 [NF_INET_PRE_ROUTING
] = 0,
44 [NF_INET_POST_ROUTING
] = sizeof(struct ipt_standard
),
45 [NF_INET_LOCAL_OUT
] = sizeof(struct ipt_standard
) * 2
48 [NF_INET_PRE_ROUTING
] = 0,
49 [NF_INET_POST_ROUTING
] = sizeof(struct ipt_standard
),
50 [NF_INET_LOCAL_OUT
] = sizeof(struct ipt_standard
) * 2
54 IPT_STANDARD_INIT(NF_ACCEPT
), /* PRE_ROUTING */
55 IPT_STANDARD_INIT(NF_ACCEPT
), /* POST_ROUTING */
56 IPT_STANDARD_INIT(NF_ACCEPT
), /* LOCAL_OUT */
58 .term
= IPT_ERROR_INIT
, /* ERROR */
61 static struct xt_table nat_table
= {
63 .valid_hooks
= NAT_VALID_HOOKS
,
64 .lock
= __RW_LOCK_UNLOCKED(nat_table
.lock
),
71 ipt_snat_target(struct sk_buff
*skb
, const struct xt_target_param
*par
)
74 enum ip_conntrack_info ctinfo
;
75 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
77 NF_CT_ASSERT(par
->hooknum
== NF_INET_POST_ROUTING
);
79 ct
= nf_ct_get(skb
, &ctinfo
);
81 /* Connection must be valid and new. */
82 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
||
83 ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
));
84 NF_CT_ASSERT(par
->out
!= NULL
);
86 return nf_nat_setup_info(ct
, &mr
->range
[0], IP_NAT_MANIP_SRC
);
90 ipt_dnat_target(struct sk_buff
*skb
, const struct xt_target_param
*par
)
93 enum ip_conntrack_info ctinfo
;
94 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
96 NF_CT_ASSERT(par
->hooknum
== NF_INET_PRE_ROUTING
||
97 par
->hooknum
== NF_INET_LOCAL_OUT
);
99 ct
= nf_ct_get(skb
, &ctinfo
);
101 /* Connection must be valid and new. */
102 NF_CT_ASSERT(ct
&& (ctinfo
== IP_CT_NEW
|| ctinfo
== IP_CT_RELATED
));
104 return nf_nat_setup_info(ct
, &mr
->range
[0], IP_NAT_MANIP_DST
);
107 static bool ipt_snat_checkentry(const struct xt_tgchk_param
*par
)
109 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
111 /* Must be a valid range */
112 if (mr
->rangesize
!= 1) {
113 printk("SNAT: multiple ranges no longer supported\n");
119 static bool ipt_dnat_checkentry(const struct xt_tgchk_param
*par
)
121 const struct nf_nat_multi_range_compat
*mr
= par
->targinfo
;
123 /* Must be a valid range */
124 if (mr
->rangesize
!= 1) {
125 printk("DNAT: multiple ranges no longer supported\n");
132 alloc_null_binding(struct nf_conn
*ct
, unsigned int hooknum
)
134 /* Force range to this IP; let proto decide mapping for
135 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
136 Use reply in case it's already been mangled (eg local packet).
139 = (HOOK2MANIP(hooknum
) == IP_NAT_MANIP_SRC
140 ? ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
141 : ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
);
142 struct nf_nat_range range
143 = { IP_NAT_RANGE_MAP_IPS
, ip
, ip
, { 0 }, { 0 } };
145 pr_debug("Allocating NULL binding for %p (%pI4)\n", ct
, &ip
);
146 return nf_nat_setup_info(ct
, &range
, HOOK2MANIP(hooknum
));
149 int nf_nat_rule_find(struct sk_buff
*skb
,
150 unsigned int hooknum
,
151 const struct net_device
*in
,
152 const struct net_device
*out
,
155 struct net
*net
= nf_ct_net(ct
);
158 ret
= ipt_do_table(skb
, hooknum
, in
, out
, net
->ipv4
.nat_table
);
160 if (ret
== NF_ACCEPT
) {
161 if (!nf_nat_initialized(ct
, HOOK2MANIP(hooknum
)))
163 ret
= alloc_null_binding(ct
, hooknum
);
168 static struct xt_target ipt_snat_reg __read_mostly
= {
170 .target
= ipt_snat_target
,
171 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
173 .hooks
= 1 << NF_INET_POST_ROUTING
,
174 .checkentry
= ipt_snat_checkentry
,
178 static struct xt_target ipt_dnat_reg __read_mostly
= {
180 .target
= ipt_dnat_target
,
181 .targetsize
= sizeof(struct nf_nat_multi_range_compat
),
183 .hooks
= (1 << NF_INET_PRE_ROUTING
) | (1 << NF_INET_LOCAL_OUT
),
184 .checkentry
= ipt_dnat_checkentry
,
188 static int __net_init
nf_nat_rule_net_init(struct net
*net
)
190 net
->ipv4
.nat_table
= ipt_register_table(net
, &nat_table
,
191 &nat_initial_table
.repl
);
192 if (IS_ERR(net
->ipv4
.nat_table
))
193 return PTR_ERR(net
->ipv4
.nat_table
);
197 static void __net_exit
nf_nat_rule_net_exit(struct net
*net
)
199 ipt_unregister_table(net
->ipv4
.nat_table
);
202 static struct pernet_operations nf_nat_rule_net_ops
= {
203 .init
= nf_nat_rule_net_init
,
204 .exit
= nf_nat_rule_net_exit
,
207 int __init
nf_nat_rule_init(void)
211 ret
= register_pernet_subsys(&nf_nat_rule_net_ops
);
214 ret
= xt_register_target(&ipt_snat_reg
);
216 goto unregister_table
;
218 ret
= xt_register_target(&ipt_dnat_reg
);
220 goto unregister_snat
;
225 xt_unregister_target(&ipt_snat_reg
);
227 unregister_pernet_subsys(&nf_nat_rule_net_ops
);
232 void nf_nat_rule_cleanup(void)
234 xt_unregister_target(&ipt_dnat_reg
);
235 xt_unregister_target(&ipt_snat_reg
);
236 unregister_pernet_subsys(&nf_nat_rule_net_ops
);