]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv4/netfilter/nf_nat_rule.c
[NETFILTER]: annotate xtables targets with const and remove casts
[mirror_ubuntu-zesty-kernel.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
JK
30
31static struct
32{
33 struct ipt_replace repl;
34 struct ipt_standard entries[3];
35 struct ipt_error term;
36} nat_initial_table __initdata = {
37 .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 = {
6e23ae2a
PM
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
3c2ad469 46 },
5b1158e9 47 .underflow = {
6e23ae2a
PM
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
3c2ad469 51 },
5b1158e9
JK
52 },
53 .entries = {
3c2ad469
PM
54 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
55 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
56 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
5b1158e9 57 },
3c2ad469 58 .term = IPT_ERROR_INIT, /* ERROR */
5b1158e9
JK
59};
60
44d34e72 61static struct xt_table __nat_table = {
5b1158e9
JK
62 .name = "nat",
63 .valid_hooks = NAT_VALID_HOOKS,
fdccecd0 64 .lock = __RW_LOCK_UNLOCKED(__nat_table.lock),
5b1158e9
JK
65 .me = THIS_MODULE,
66 .af = AF_INET,
67};
44d34e72 68static struct xt_table *nat_table;
5b1158e9
JK
69
70/* Source NAT */
3db05fea 71static unsigned int ipt_snat_target(struct sk_buff *skb,
5b1158e9
JK
72 const struct net_device *in,
73 const struct net_device *out,
74 unsigned int hooknum,
75 const struct xt_target *target,
76 const void *targinfo)
77{
78 struct nf_conn *ct;
79 enum ip_conntrack_info ctinfo;
80 const struct nf_nat_multi_range_compat *mr = targinfo;
81
6e23ae2a 82 NF_CT_ASSERT(hooknum == NF_INET_POST_ROUTING);
5b1158e9 83
3db05fea 84 ct = nf_ct_get(skb, &ctinfo);
5b1158e9
JK
85
86 /* Connection must be valid and new. */
87 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
e905a9ed 88 ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
5b1158e9
JK
89 NF_CT_ASSERT(out);
90
cc01dcbd 91 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
5b1158e9
JK
92}
93
94/* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
95static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
96{
97 static int warned = 0;
98 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
99 struct rtable *rt;
100
f206351a 101 if (ip_route_output_key(&init_net, &rt, &fl) != 0)
5b1158e9
JK
102 return;
103
104 if (rt->rt_src != srcip && !warned) {
105 printk("NAT: no longer support implicit source local NAT\n");
106 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
107 NIPQUAD(srcip), NIPQUAD(dstip));
108 warned = 1;
109 }
110 ip_rt_put(rt);
111}
112
3db05fea 113static unsigned int ipt_dnat_target(struct sk_buff *skb,
5b1158e9
JK
114 const struct net_device *in,
115 const struct net_device *out,
116 unsigned int hooknum,
117 const struct xt_target *target,
118 const void *targinfo)
119{
120 struct nf_conn *ct;
121 enum ip_conntrack_info ctinfo;
122 const struct nf_nat_multi_range_compat *mr = targinfo;
123
6e23ae2a
PM
124 NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING ||
125 hooknum == NF_INET_LOCAL_OUT);
5b1158e9 126
3db05fea 127 ct = nf_ct_get(skb, &ctinfo);
5b1158e9
JK
128
129 /* Connection must be valid and new. */
130 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
131
6e23ae2a 132 if (hooknum == NF_INET_LOCAL_OUT &&
5b1158e9 133 mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
3db05fea 134 warn_if_extra_mangle(ip_hdr(skb)->daddr,
5b1158e9
JK
135 mr->range[0].min_ip);
136
cc01dcbd 137 return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_DST);
5b1158e9
JK
138}
139
e1931b78
JE
140static bool ipt_snat_checkentry(const char *tablename,
141 const void *entry,
142 const struct xt_target *target,
143 void *targinfo,
144 unsigned int hook_mask)
5b1158e9 145{
3cf93c96 146 const struct nf_nat_multi_range_compat *mr = targinfo;
5b1158e9
JK
147
148 /* Must be a valid range */
149 if (mr->rangesize != 1) {
150 printk("SNAT: multiple ranges no longer supported\n");
e1931b78 151 return false;
5b1158e9 152 }
e1931b78 153 return true;
5b1158e9
JK
154}
155
e1931b78
JE
156static bool ipt_dnat_checkentry(const char *tablename,
157 const void *entry,
158 const struct xt_target *target,
159 void *targinfo,
160 unsigned int hook_mask)
5b1158e9 161{
3cf93c96 162 const struct nf_nat_multi_range_compat *mr = targinfo;
5b1158e9
JK
163
164 /* Must be a valid range */
165 if (mr->rangesize != 1) {
166 printk("DNAT: multiple ranges no longer supported\n");
e1931b78 167 return false;
5b1158e9 168 }
e1931b78 169 return true;
5b1158e9
JK
170}
171
170b197c 172unsigned int
ba4c7cba 173alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
5b1158e9
JK
174{
175 /* Force range to this IP; let proto decide mapping for
176 per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
177 Use reply in case it's already been mangled (eg local packet).
178 */
179 __be32 ip
180 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
181 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
182 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
183 struct nf_nat_range range
184 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
185
0d53778e
PM
186 pr_debug("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
187 ct, NIPQUAD(ip));
cc01dcbd 188 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
5b1158e9
JK
189}
190
191unsigned int
ba4c7cba 192alloc_null_binding_confirmed(struct nf_conn *ct, unsigned int hooknum)
5b1158e9
JK
193{
194 __be32 ip
195 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
196 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
197 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
a34c4589 198 __be16 all
5b1158e9
JK
199 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
200 ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
201 : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
202 struct nf_nat_range range
203 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
204
0d53778e
PM
205 pr_debug("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
206 ct, NIPQUAD(ip));
cc01dcbd 207 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
5b1158e9
JK
208}
209
3db05fea 210int nf_nat_rule_find(struct sk_buff *skb,
5b1158e9
JK
211 unsigned int hooknum,
212 const struct net_device *in,
213 const struct net_device *out,
ba4c7cba 214 struct nf_conn *ct)
5b1158e9
JK
215{
216 int ret;
217
44d34e72 218 ret = ipt_do_table(skb, hooknum, in, out, nat_table);
5b1158e9
JK
219
220 if (ret == NF_ACCEPT) {
221 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
222 /* NUL mapping */
ba4c7cba 223 ret = alloc_null_binding(ct, hooknum);
5b1158e9
JK
224 }
225 return ret;
226}
227
9f15c530 228static struct xt_target ipt_snat_reg __read_mostly = {
5b1158e9
JK
229 .name = "SNAT",
230 .target = ipt_snat_target,
231 .targetsize = sizeof(struct nf_nat_multi_range_compat),
232 .table = "nat",
6e23ae2a 233 .hooks = 1 << NF_INET_POST_ROUTING,
5b1158e9
JK
234 .checkentry = ipt_snat_checkentry,
235 .family = AF_INET,
236};
237
9f15c530 238static struct xt_target ipt_dnat_reg __read_mostly = {
5b1158e9
JK
239 .name = "DNAT",
240 .target = ipt_dnat_target,
241 .targetsize = sizeof(struct nf_nat_multi_range_compat),
242 .table = "nat",
6e23ae2a 243 .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT),
5b1158e9
JK
244 .checkentry = ipt_dnat_checkentry,
245 .family = AF_INET,
246};
247
248int __init nf_nat_rule_init(void)
249{
250 int ret;
251
44d34e72
AD
252 nat_table = ipt_register_table(&init_net, &__nat_table,
253 &nat_initial_table.repl);
254 if (IS_ERR(nat_table))
255 return PTR_ERR(nat_table);
5b1158e9
JK
256 ret = xt_register_target(&ipt_snat_reg);
257 if (ret != 0)
258 goto unregister_table;
259
260 ret = xt_register_target(&ipt_dnat_reg);
261 if (ret != 0)
262 goto unregister_snat;
263
264 return ret;
265
266 unregister_snat:
267 xt_unregister_target(&ipt_snat_reg);
268 unregister_table:
44d34e72 269 ipt_unregister_table(nat_table);
5b1158e9
JK
270
271 return ret;
272}
273
274void nf_nat_rule_cleanup(void)
275{
276 xt_unregister_target(&ipt_dnat_reg);
277 xt_unregister_target(&ipt_snat_reg);
44d34e72 278 ipt_unregister_table(nat_table);
5b1158e9 279}