Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.git] / net / ipv4 / netfilter / nf_nat_rule.c
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
27 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
32
33 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
34
35 static struct
36 {
37         struct ipt_replace repl;
38         struct ipt_standard entries[3];
39         struct ipt_error term;
40 } nat_initial_table __initdata = {
41         .repl = {
42                 .name = "nat",
43                 .valid_hooks = NAT_VALID_HOOKS,
44                 .num_entries = 4,
45                 .size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
46                 .hook_entry = {
47                         [NF_IP_PRE_ROUTING] = 0,
48                         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
49                         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
50                 .underflow = {
51                         [NF_IP_PRE_ROUTING] = 0,
52                         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
53                         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
54         },
55         .entries = {
56                 /* PRE_ROUTING */
57                 {
58                         .entry = {
59                                 .target_offset = sizeof(struct ipt_entry),
60                                 .next_offset = sizeof(struct ipt_standard),
61                         },
62                         .target = {
63                                 .target = {
64                                         .u = {
65                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
66                                         },
67                                 },
68                                 .verdict = -NF_ACCEPT - 1,
69                         },
70                 },
71                 /* POST_ROUTING */
72                 {
73                         .entry = {
74                                 .target_offset = sizeof(struct ipt_entry),
75                                 .next_offset = sizeof(struct ipt_standard),
76                         },
77                         .target = {
78                                 .target = {
79                                         .u = {
80                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
81                                         },
82                                 },
83                                 .verdict = -NF_ACCEPT - 1,
84                         },
85                 },
86                 /* LOCAL_OUT */
87                 {
88                         .entry = {
89                                 .target_offset = sizeof(struct ipt_entry),
90                                 .next_offset = sizeof(struct ipt_standard),
91                         },
92                         .target = {
93                                 .target = {
94                                         .u = {
95                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_standard_target)),
96                                         },
97                                 },
98                                 .verdict = -NF_ACCEPT - 1,
99                         },
100                 },
101         },
102         /* ERROR */
103         .term = {
104                 .entry = {
105                         .target_offset = sizeof(struct ipt_entry),
106                         .next_offset = sizeof(struct ipt_error),
107                 },
108                 .target = {
109                         .target = {
110                                 .u = {
111                                         .user = {
112                                                 .target_size = IPT_ALIGN(sizeof(struct ipt_error_target)),
113                                                 .name = IPT_ERROR_TARGET,
114                                         },
115                                 },
116                         },
117                         .errorname = "ERROR",
118                 },
119         }
120 };
121
122 static struct xt_table nat_table = {
123         .name           = "nat",
124         .valid_hooks    = NAT_VALID_HOOKS,
125         .lock           = RW_LOCK_UNLOCKED,
126         .me             = THIS_MODULE,
127         .af             = AF_INET,
128 };
129
130 /* Source NAT */
131 static unsigned int ipt_snat_target(struct sk_buff **pskb,
132                                     const struct net_device *in,
133                                     const struct net_device *out,
134                                     unsigned int hooknum,
135                                     const struct xt_target *target,
136                                     const void *targinfo)
137 {
138         struct nf_conn *ct;
139         enum ip_conntrack_info ctinfo;
140         const struct nf_nat_multi_range_compat *mr = targinfo;
141
142         NF_CT_ASSERT(hooknum == NF_IP_POST_ROUTING);
143
144         ct = nf_ct_get(*pskb, &ctinfo);
145
146         /* Connection must be valid and new. */
147         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
148                             ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
149         NF_CT_ASSERT(out);
150
151         return nf_nat_setup_info(ct, &mr->range[0], hooknum);
152 }
153
154 /* Before 2.6.11 we did implicit source NAT if required. Warn about change. */
155 static void warn_if_extra_mangle(__be32 dstip, __be32 srcip)
156 {
157         static int warned = 0;
158         struct flowi fl = { .nl_u = { .ip4_u = { .daddr = dstip } } };
159         struct rtable *rt;
160
161         if (ip_route_output_key(&rt, &fl) != 0)
162                 return;
163
164         if (rt->rt_src != srcip && !warned) {
165                 printk("NAT: no longer support implicit source local NAT\n");
166                 printk("NAT: packet src %u.%u.%u.%u -> dst %u.%u.%u.%u\n",
167                        NIPQUAD(srcip), NIPQUAD(dstip));
168                 warned = 1;
169         }
170         ip_rt_put(rt);
171 }
172
173 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
174                                     const struct net_device *in,
175                                     const struct net_device *out,
176                                     unsigned int hooknum,
177                                     const struct xt_target *target,
178                                     const void *targinfo)
179 {
180         struct nf_conn *ct;
181         enum ip_conntrack_info ctinfo;
182         const struct nf_nat_multi_range_compat *mr = targinfo;
183
184         NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
185                      hooknum == NF_IP_LOCAL_OUT);
186
187         ct = nf_ct_get(*pskb, &ctinfo);
188
189         /* Connection must be valid and new. */
190         NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
191
192         if (hooknum == NF_IP_LOCAL_OUT &&
193             mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)
194                 warn_if_extra_mangle((*pskb)->nh.iph->daddr,
195                                      mr->range[0].min_ip);
196
197         return nf_nat_setup_info(ct, &mr->range[0], hooknum);
198 }
199
200 static int ipt_snat_checkentry(const char *tablename,
201                                const void *entry,
202                                const struct xt_target *target,
203                                void *targinfo,
204                                unsigned int hook_mask)
205 {
206         struct nf_nat_multi_range_compat *mr = targinfo;
207
208         /* Must be a valid range */
209         if (mr->rangesize != 1) {
210                 printk("SNAT: multiple ranges no longer supported\n");
211                 return 0;
212         }
213         return 1;
214 }
215
216 static int ipt_dnat_checkentry(const char *tablename,
217                                const void *entry,
218                                const struct xt_target *target,
219                                void *targinfo,
220                                unsigned int hook_mask)
221 {
222         struct nf_nat_multi_range_compat *mr = targinfo;
223
224         /* Must be a valid range */
225         if (mr->rangesize != 1) {
226                 printk("DNAT: multiple ranges no longer supported\n");
227                 return 0;
228         }
229         if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM) {
230                 printk("DNAT: port randomization not supported\n");
231                 return 0;
232         }
233         return 1;
234 }
235
236 inline unsigned int
237 alloc_null_binding(struct nf_conn *ct,
238                    struct nf_nat_info *info,
239                    unsigned int hooknum)
240 {
241         /* Force range to this IP; let proto decide mapping for
242            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
243            Use reply in case it's already been mangled (eg local packet).
244         */
245         __be32 ip
246                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
247                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
248                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
249         struct nf_nat_range range
250                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } };
251
252         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n",
253                ct, NIPQUAD(ip));
254         return nf_nat_setup_info(ct, &range, hooknum);
255 }
256
257 unsigned int
258 alloc_null_binding_confirmed(struct nf_conn *ct,
259                              struct nf_nat_info *info,
260                              unsigned int hooknum)
261 {
262         __be32 ip
263                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
264                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip
265                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip);
266         u_int16_t all
267                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
268                    ? ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.all
269                    : ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u.all);
270         struct nf_nat_range range
271                 = { IP_NAT_RANGE_MAP_IPS, ip, ip, { all }, { all } };
272
273         DEBUGP("Allocating NULL binding for confirmed %p (%u.%u.%u.%u)\n",
274                ct, NIPQUAD(ip));
275         return nf_nat_setup_info(ct, &range, hooknum);
276 }
277
278 int nf_nat_rule_find(struct sk_buff **pskb,
279                      unsigned int hooknum,
280                      const struct net_device *in,
281                      const struct net_device *out,
282                      struct nf_conn *ct,
283                      struct nf_nat_info *info)
284 {
285         int ret;
286
287         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table);
288
289         if (ret == NF_ACCEPT) {
290                 if (!nf_nat_initialized(ct, HOOK2MANIP(hooknum)))
291                         /* NUL mapping */
292                         ret = alloc_null_binding(ct, info, hooknum);
293         }
294         return ret;
295 }
296
297 static struct xt_target ipt_snat_reg = {
298         .name           = "SNAT",
299         .target         = ipt_snat_target,
300         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
301         .table          = "nat",
302         .hooks          = 1 << NF_IP_POST_ROUTING,
303         .checkentry     = ipt_snat_checkentry,
304         .family         = AF_INET,
305 };
306
307 static struct xt_target ipt_dnat_reg = {
308         .name           = "DNAT",
309         .target         = ipt_dnat_target,
310         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
311         .table          = "nat",
312         .hooks          = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT),
313         .checkentry     = ipt_dnat_checkentry,
314         .family         = AF_INET,
315 };
316
317 int __init nf_nat_rule_init(void)
318 {
319         int ret;
320
321         ret = ipt_register_table(&nat_table, &nat_initial_table.repl);
322         if (ret != 0)
323                 return ret;
324         ret = xt_register_target(&ipt_snat_reg);
325         if (ret != 0)
326                 goto unregister_table;
327
328         ret = xt_register_target(&ipt_dnat_reg);
329         if (ret != 0)
330                 goto unregister_snat;
331
332         return ret;
333
334  unregister_snat:
335         xt_unregister_target(&ipt_snat_reg);
336  unregister_table:
337         ipt_unregister_table(&nat_table);
338
339         return ret;
340 }
341
342 void nf_nat_rule_cleanup(void)
343 {
344         xt_unregister_target(&ipt_dnat_reg);
345         xt_unregister_target(&ipt_snat_reg);
346         ipt_unregister_table(&nat_table);
347 }