added files
[bcm963xx.git] / kernel / linux / net / ipv4 / netfilter / ip_nat_rule.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 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 <linux/bitops.h>
20
21 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
22 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
23
24 #include <linux/netfilter_ipv4/ip_tables.h>
25 #include <linux/netfilter_ipv4/ip_nat.h>
26 #include <linux/netfilter_ipv4/ip_nat_core.h>
27 #include <linux/netfilter_ipv4/ip_nat_rule.h>
28 #include <linux/netfilter_ipv4/listhelp.h>
29
30 #if 0
31 #define DEBUGP printk
32 #else
33 #define DEBUGP(format, args...)
34 #endif
35
36 #define NAT_VALID_HOOKS ((1<<NF_IP_PRE_ROUTING) | (1<<NF_IP_POST_ROUTING) | (1<<NF_IP_LOCAL_OUT))
37
38 /* Standard entry. */
39 struct ipt_standard
40 {
41         struct ipt_entry entry;
42         struct ipt_standard_target target;
43 };
44
45 struct ipt_error_target
46 {
47         struct ipt_entry_target target;
48         char errorname[IPT_FUNCTION_MAXNAMELEN];
49 };
50
51 struct ipt_error
52 {
53         struct ipt_entry entry;
54         struct ipt_error_target target;
55 };
56
57 static struct
58 {
59         struct ipt_replace repl;
60         struct ipt_standard entries[3];
61         struct ipt_error term;
62 } nat_initial_table __initdata
63 = { { "nat", NAT_VALID_HOOKS, 4,
64       sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
65       { [NF_IP_PRE_ROUTING] = 0,
66         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
67         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
68       { [NF_IP_PRE_ROUTING] = 0,
69         [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard),
70         [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 2 },
71       0, NULL, { } },
72     {
73             /* PRE_ROUTING */
74             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
75                 0,
76                 sizeof(struct ipt_entry),
77                 sizeof(struct ipt_standard),
78                 0, { 0, 0 }, { } },
79               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
80                 -NF_ACCEPT - 1 } },
81             /* POST_ROUTING */
82             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
83                 0,
84                 sizeof(struct ipt_entry),
85                 sizeof(struct ipt_standard),
86                 0, { 0, 0 }, { } },
87               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
88                 -NF_ACCEPT - 1 } },
89             /* LOCAL_OUT */
90             { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
91                 0,
92                 sizeof(struct ipt_entry),
93                 sizeof(struct ipt_standard),
94                 0, { 0, 0 }, { } },
95               { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
96                 -NF_ACCEPT - 1 } }
97     },
98     /* ERROR */
99     { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
100         0,
101         sizeof(struct ipt_entry),
102         sizeof(struct ipt_error),
103         0, { 0, 0 }, { } },
104       { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
105           { } },
106         "ERROR"
107       }
108     }
109 };
110
111 static struct ipt_table nat_table = {
112         .name           = "nat",
113         .table          = &nat_initial_table.repl,
114         .valid_hooks    = NAT_VALID_HOOKS,
115         .lock           = RW_LOCK_UNLOCKED,
116         .me             = THIS_MODULE,
117 };
118
119 /* Source NAT */
120 static unsigned int ipt_snat_target(struct sk_buff **pskb,
121                                     const struct net_device *in,
122                                     const struct net_device *out,
123                                     unsigned int hooknum,
124                                     const void *targinfo,
125                                     void *userinfo)
126 {
127         struct ip_conntrack *ct;
128         enum ip_conntrack_info ctinfo;
129
130         IP_NF_ASSERT(hooknum == NF_IP_POST_ROUTING);
131
132         ct = ip_conntrack_get(*pskb, &ctinfo);
133
134         /* Connection must be valid and new. */
135         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
136         IP_NF_ASSERT(out);
137
138         return ip_nat_setup_info(ct, targinfo, hooknum);
139 }
140
141 static unsigned int ipt_dnat_target(struct sk_buff **pskb,
142                                     const struct net_device *in,
143                                     const struct net_device *out,
144                                     unsigned int hooknum,
145                                     const void *targinfo,
146                                     void *userinfo)
147 {
148         struct ip_conntrack *ct;
149         enum ip_conntrack_info ctinfo;
150
151 #ifdef CONFIG_IP_NF_NAT_LOCAL
152         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
153                      || hooknum == NF_IP_LOCAL_OUT);
154 #else
155         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING);
156 #endif
157
158         ct = ip_conntrack_get(*pskb, &ctinfo);
159
160         /* Connection must be valid and new. */
161         IP_NF_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
162
163         return ip_nat_setup_info(ct, targinfo, hooknum);
164 }
165
166 static int ipt_snat_checkentry(const char *tablename,
167                                const struct ipt_entry *e,
168                                void *targinfo,
169                                unsigned int targinfosize,
170                                unsigned int hook_mask)
171 {
172         struct ip_nat_multi_range *mr = targinfo;
173
174         /* Must be a valid range */
175         if (targinfosize < sizeof(struct ip_nat_multi_range)) {
176                 DEBUGP("SNAT: Target size %u too small\n", targinfosize);
177                 return 0;
178         }
179
180         if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
181                                        + (sizeof(struct ip_nat_range)
182                                           * (mr->rangesize - 1))))) {
183                 DEBUGP("SNAT: Target size %u wrong for %u ranges\n",
184                        targinfosize, mr->rangesize);
185                 return 0;
186         }
187
188         /* Only allow these for NAT. */
189         if (strcmp(tablename, "nat") != 0) {
190                 DEBUGP("SNAT: wrong table %s\n", tablename);
191                 return 0;
192         }
193
194         if (hook_mask & ~(1 << NF_IP_POST_ROUTING)) {
195                 DEBUGP("SNAT: hook mask 0x%x bad\n", hook_mask);
196                 return 0;
197         }
198         return 1;
199 }
200
201 static int ipt_dnat_checkentry(const char *tablename,
202                                const struct ipt_entry *e,
203                                void *targinfo,
204                                unsigned int targinfosize,
205                                unsigned int hook_mask)
206 {
207         struct ip_nat_multi_range *mr = targinfo;
208
209         /* Must be a valid range */
210         if (targinfosize < sizeof(struct ip_nat_multi_range)) {
211                 DEBUGP("DNAT: Target size %u too small\n", targinfosize);
212                 return 0;
213         }
214
215         if (targinfosize != IPT_ALIGN((sizeof(struct ip_nat_multi_range)
216                                        + (sizeof(struct ip_nat_range)
217                                           * (mr->rangesize - 1))))) {
218                 DEBUGP("DNAT: Target size %u wrong for %u ranges\n",
219                        targinfosize, mr->rangesize);
220                 return 0;
221         }
222
223         /* Only allow these for NAT. */
224         if (strcmp(tablename, "nat") != 0) {
225                 DEBUGP("DNAT: wrong table %s\n", tablename);
226                 return 0;
227         }
228
229         if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_OUT))) {
230                 DEBUGP("DNAT: hook mask 0x%x bad\n", hook_mask);
231                 return 0;
232         }
233         
234 #ifndef CONFIG_IP_NF_NAT_LOCAL
235         if (hook_mask & (1 << NF_IP_LOCAL_OUT)) {
236                 DEBUGP("DNAT: CONFIG_IP_NF_NAT_LOCAL not enabled\n");
237                 return 0;
238         }
239 #endif
240
241         return 1;
242 }
243
244 inline unsigned int
245 alloc_null_binding(struct ip_conntrack *conntrack,
246                    struct ip_nat_info *info,
247                    unsigned int hooknum)
248 {
249         /* Force range to this IP; let proto decide mapping for
250            per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
251            Use reply in case it's already been mangled (eg local packet).
252         */
253         u_int32_t ip
254                 = (HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
255                    ? conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip
256                    : conntrack->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip);
257         struct ip_nat_multi_range mr
258                 = { 1, { { IP_NAT_RANGE_MAP_IPS, ip, ip, { 0 }, { 0 } } } };
259
260         DEBUGP("Allocating NULL binding for %p (%u.%u.%u.%u)\n", conntrack,
261                NIPQUAD(ip));
262         return ip_nat_setup_info(conntrack, &mr, hooknum);
263 }
264
265 int ip_nat_rule_find(struct sk_buff **pskb,
266                      unsigned int hooknum,
267                      const struct net_device *in,
268                      const struct net_device *out,
269                      struct ip_conntrack *ct,
270                      struct ip_nat_info *info)
271 {
272         int ret;
273
274         ret = ipt_do_table(pskb, hooknum, in, out, &nat_table, NULL);
275
276         if (ret == NF_ACCEPT) {
277                 if (!(info->initialized & (1 << HOOK2MANIP(hooknum))))
278                         /* NUL mapping */
279                         ret = alloc_null_binding(ct, info, hooknum);
280         }
281         return ret;
282 }
283
284 static struct ipt_target ipt_snat_reg = {
285         .name           = "SNAT",
286         .target         = ipt_snat_target,
287         .checkentry     = ipt_snat_checkentry,
288 };
289
290 static struct ipt_target ipt_dnat_reg = {
291         .name           = "DNAT",
292         .target         = ipt_dnat_target,
293         .checkentry     = ipt_dnat_checkentry,
294 };
295
296 int __init ip_nat_rule_init(void)
297 {
298         int ret;
299
300         ret = ipt_register_table(&nat_table);
301         if (ret != 0)
302                 return ret;
303         ret = ipt_register_target(&ipt_snat_reg);
304         if (ret != 0)
305                 goto unregister_table;
306
307         ret = ipt_register_target(&ipt_dnat_reg);
308         if (ret != 0)
309                 goto unregister_snat;
310
311         return ret;
312
313  unregister_snat:
314         ipt_unregister_target(&ipt_snat_reg);
315  unregister_table:
316         ipt_unregister_table(&nat_table);
317
318         return ret;
319 }
320
321 void ip_nat_rule_cleanup(void)
322 {
323         ipt_unregister_target(&ipt_dnat_reg);
324         ipt_unregister_target(&ipt_snat_reg);
325         ipt_unregister_table(&nat_table);
326 }