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