blob: 83170ff131f984d3ec8f4e436654922e8efe6da9 [file] [log] [blame]
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001/* (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
Patrick McHardy6e23ae22007-11-19 18:53:30 -080027#define NAT_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
28 (1 << NF_INET_POST_ROUTING) | \
29 (1 << NF_INET_LOCAL_OUT))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080030
31static struct
32{
33 struct ipt_replace repl;
34 struct ipt_standard entries[3];
35 struct ipt_error term;
Alexey Dobriyane099a172008-10-08 11:35:10 +020036} nat_initial_table __net_initdata = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080037 .repl = {
38 .name = "nat",
39 .valid_hooks = NAT_VALID_HOOKS,
40 .num_entries = 4,
41 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
42 .hook_entry = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080043 [NF_INET_PRE_ROUTING] = 0,
44 [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
45 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
Patrick McHardy3c2ad462007-05-10 14:14:16 -070046 },
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080047 .underflow = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080048 [NF_INET_PRE_ROUTING] = 0,
49 [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard),
50 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2
Patrick McHardy3c2ad462007-05-10 14:14:16 -070051 },
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080052 },
53 .entries = {
Patrick McHardy3c2ad462007-05-10 14:14:16 -070054 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
55 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
56 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080057 },
Patrick McHardy3c2ad462007-05-10 14:14:16 -070058 .term = IPT_ERROR_INIT, /* ERROR */
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080059};
60
Alexey Dobriyane099a172008-10-08 11:35:10 +020061static struct xt_table nat_table = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080062 .name = "nat",
63 .valid_hooks = NAT_VALID_HOOKS,
Robert P. J. Dayfdccecd2008-04-14 09:56:03 +020064 .lock = __RW_LOCK_UNLOCKED(__nat_table.lock),
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080065 .me = THIS_MODULE,
66 .af = AF_INET,
67};
68
69/* Source NAT */
Jan Engelhardt7eb35582008-10-08 11:35:19 +020070static unsigned int
71ipt_snat_target(struct sk_buff *skb, const struct xt_target_param *par)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080072{
73 struct nf_conn *ct;
74 enum ip_conntrack_info ctinfo;
Jan Engelhardt7eb35582008-10-08 11:35:19 +020075 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080076
Jan Engelhardt7eb35582008-10-08 11:35:19 +020077 NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080078
Herbert Xu3db05fe2007-10-15 00:53:15 -070079 ct = nf_ct_get(skb, &ctinfo);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080080
81 /* Connection must be valid and new. */
82 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090083 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
Jan Engelhardt7eb35582008-10-08 11:35:19 +020084 NF_CT_ASSERT(par->out != NULL);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080085
Patrick McHardycc01dcb2007-12-17 22:38:20 -080086 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080087}
88
89/* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
Alexey Dobriyan9174c152008-10-08 11:35:11 +020090static void warn_if_extra_mangle(struct net *net, __be32 dstip, __be32 srcip)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080091{
92 static int warned = 0;
93 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
94 struct rtable *rt;
95
Alexey Dobriyan9174c152008-10-08 11:35:11 +020096 if (ip_route_output_key(net, &rt, &fl) != 0)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080097 return;
98
99 if (rt->rt_src != srcip && !warned) {
100 printk("NAT: no longer support implicit source local NAT\n");
101 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
102 NIPQUAD(srcip), NIPQUAD(dstip));
103 warned = 1;
104 }
105 ip_rt_put(rt);
106}
107
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200108static unsigned int
109ipt_dnat_target(struct sk_buff *skb, const struct xt_target_param *par)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800110{
111 struct nf_conn *ct;
112 enum ip_conntrack_info ctinfo;
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200113 const struct nf_nat_multi_range_compat *mr = par->targinfo;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800114
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200115 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
116 par->hooknum == NF_INET_LOCAL_OUT);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800117
Herbert Xu3db05fe2007-10-15 00:53:15 -0700118 ct = nf_ct_get(skb, &ctinfo);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800119
120 /* Connection must be valid and new. */
121 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
122
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200123 if (par->hooknum == NF_INET_LOCAL_OUT &&
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800124 mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200125 warn_if_extra_mangle(dev_net(par->out), ip_hdr(skb)->daddr,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800126 mr->range[0].min_ip);
127
Patrick McHardycc01dcb2007-12-17 22:38:20 -0800128 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800129}
130
Jan Engelhardte1931b72007-07-07 22:16:26 -0700131static bool ipt_snat_checkentry(const char *tablename,
132 const void *entry,
133 const struct xt_target *target,
134 void *targinfo,
135 unsigned int hook_mask)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800136{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200137 const struct nf_nat_multi_range_compat *mr = targinfo;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800138
139 /* Must be a valid range */
140 if (mr->rangesize != 1) {
141 printk("SNAT: multiple ranges no longer supported\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700142 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800143 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700144 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800145}
146
Jan Engelhardte1931b72007-07-07 22:16:26 -0700147static bool ipt_dnat_checkentry(const char *tablename,
148 const void *entry,
149 const struct xt_target *target,
150 void *targinfo,
151 unsigned int hook_mask)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800152{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200153 const struct nf_nat_multi_range_compat *mr = targinfo;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800154
155 /* Must be a valid range */
156 if (mr->rangesize != 1) {
157 printk("DNAT: multiple ranges no longer supported\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700158 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800159 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700160 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800161}
162
Jan Engelhardt170b1972007-07-07 22:17:36 -0700163unsigned int
Yasuyuki Kozakaiba4c7cb2007-05-10 14:14:45 -0700164alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800165{
166 /* Force range to this IP; let proto decide mapping for
167 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
168 Use reply in case it's already been mangled (eg local packet).
169 */
170 __be32 ip
171 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
172 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
173 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
174 struct nf_nat_range range
175 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
176
Patrick McHardy0d537782007-07-07 22:39:38 -0700177 pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
178 ct, NIPQUAD(ip));
Patrick McHardycc01dcb2007-12-17 22:38:20 -0800179 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800180}
181
Herbert Xu3db05fe2007-10-15 00:53:15 -0700182int nf_nat_rule_find(struct sk_buff *skb,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800183 unsigned int hooknum,
184 const struct net_device *in,
185 const struct net_device *out,
Yasuyuki Kozakaiba4c7cb2007-05-10 14:14:45 -0700186 struct nf_conn *ct)
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800187{
Alexey Dobriyane099a172008-10-08 11:35:10 +0200188 struct net *net = nf_ct_net(ct);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800189 int ret;
190
Alexey Dobriyane099a172008-10-08 11:35:10 +0200191 ret = ipt_do_table(skb, hooknum, in, out, net->ipv4.nat_table);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800192
193 if (ret == NF_ACCEPT) {
194 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
195 /* NUL mapping */
Yasuyuki Kozakaiba4c7cb2007-05-10 14:14:45 -0700196 ret = alloc_null_binding(ct, hooknum);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800197 }
198 return ret;
199}
200
Patrick McHardy9f15c532007-07-07 22:22:02 -0700201static struct xt_target ipt_snat_reg __read_mostly = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800202 .name = "SNAT",
203 .target = ipt_snat_target,
204 .targetsize = sizeof(struct nf_nat_multi_range_compat),
205 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800206 .hooks = 1 << NF_INET_POST_ROUTING,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800207 .checkentry = ipt_snat_checkentry,
208 .family = AF_INET,
209};
210
Patrick McHardy9f15c532007-07-07 22:22:02 -0700211static struct xt_target ipt_dnat_reg __read_mostly = {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800212 .name = "DNAT",
213 .target = ipt_dnat_target,
214 .targetsize = sizeof(struct nf_nat_multi_range_compat),
215 .table = "nat",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800216 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800217 .checkentry = ipt_dnat_checkentry,
218 .family = AF_INET,
219};
220
Alexey Dobriyane099a172008-10-08 11:35:10 +0200221static int __net_init nf_nat_rule_net_init(struct net *net)
222{
223 net->ipv4.nat_table = ipt_register_table(net, &nat_table,
224 &nat_initial_table.repl);
225 if (IS_ERR(net->ipv4.nat_table))
226 return PTR_ERR(net->ipv4.nat_table);
227 return 0;
228}
229
230static void __net_exit nf_nat_rule_net_exit(struct net *net)
231{
232 ipt_unregister_table(net->ipv4.nat_table);
233}
234
235static struct pernet_operations nf_nat_rule_net_ops = {
236 .init = nf_nat_rule_net_init,
237 .exit = nf_nat_rule_net_exit,
238};
239
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800240int __init nf_nat_rule_init(void)
241{
242 int ret;
243
Alexey Dobriyane099a172008-10-08 11:35:10 +0200244 ret = register_pernet_subsys(&nf_nat_rule_net_ops);
245 if (ret != 0)
246 goto out;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800247 ret = xt_register_target(&ipt_snat_reg);
248 if (ret != 0)
249 goto unregister_table;
250
251 ret = xt_register_target(&ipt_dnat_reg);
252 if (ret != 0)
253 goto unregister_snat;
254
255 return ret;
256
257 unregister_snat:
258 xt_unregister_target(&ipt_snat_reg);
259 unregister_table:
Alexey Dobriyane099a172008-10-08 11:35:10 +0200260 unregister_pernet_subsys(&nf_nat_rule_net_ops);
261 out:
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800262 return ret;
263}
264
265void nf_nat_rule_cleanup(void)
266{
267 xt_unregister_target(&ipt_dnat_reg);
268 xt_unregister_target(&ipt_snat_reg);
Alexey Dobriyane099a172008-10-08 11:35:10 +0200269 unregister_pernet_subsys(&nf_nat_rule_net_ops);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800270}