vserver 1.9.3
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_standalone.c
1 /* This file contains all the functions required for the standalone
2    ip_nat module.
3
4    These are not required by the compatibility layer.
5 */
6
7 /* (C) 1999-2001 Paul `Rusty' Russell
8  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 /*
16  * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
17  *      - new API and handling of conntrack/nat helpers
18  *      - now capable of multiple expectations for one master
19  * */
20
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/icmp.h>
24 #include <linux/ip.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter_ipv4.h>
27 #include <linux/module.h>
28 #include <linux/skbuff.h>
29 #include <linux/proc_fs.h>
30 #include <net/ip.h>
31 #include <net/checksum.h>
32 #include <linux/spinlock.h>
33
34 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_nat_lock)
35 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_nat_lock)
36
37 #include <linux/netfilter_ipv4/ip_nat.h>
38 #include <linux/netfilter_ipv4/ip_nat_rule.h>
39 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_core.h>
41 #include <linux/netfilter_ipv4/ip_nat_helper.h>
42 #include <linux/netfilter_ipv4/ip_tables.h>
43 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
44 #include <linux/netfilter_ipv4/listhelp.h>
45
46 #if 0
47 #define DEBUGP printk
48 #else
49 #define DEBUGP(format, args...)
50 #endif
51
52 #define HOOKNAME(hooknum) ((hooknum) == NF_IP_POST_ROUTING ? "POST_ROUTING"  \
53                            : ((hooknum) == NF_IP_PRE_ROUTING ? "PRE_ROUTING" \
54                               : ((hooknum) == NF_IP_LOCAL_OUT ? "LOCAL_OUT"  \
55                                  : ((hooknum) == NF_IP_LOCAL_IN ? "LOCAL_IN"  \
56                                     : "*ERROR*")))
57
58 static inline int call_expect(struct ip_conntrack *master,
59                               struct sk_buff **pskb,
60                               unsigned int hooknum,
61                               struct ip_conntrack *ct,
62                               struct ip_nat_info *info)
63 {
64         return master->nat.info.helper->expect(pskb, hooknum, ct, info);
65 }
66
67 static unsigned int
68 ip_nat_fn(unsigned int hooknum,
69           struct sk_buff **pskb,
70           const struct net_device *in,
71           const struct net_device *out,
72           int (*okfn)(struct sk_buff *))
73 {
74         struct ip_conntrack *ct;
75         enum ip_conntrack_info ctinfo;
76         struct ip_nat_info *info;
77         /* maniptype == SRC for postrouting. */
78         enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
79
80         /* We never see fragments: conntrack defrags on pre-routing
81            and local-out, and ip_nat_out protects post-routing. */
82         IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
83                        & htons(IP_MF|IP_OFFSET)));
84
85         (*pskb)->nfcache |= NFC_UNKNOWN;
86
87         /* If we had a hardware checksum before, it's now invalid */
88         if ((*pskb)->ip_summed == CHECKSUM_HW)
89                 if (skb_checksum_help(pskb, (out == NULL)))
90                         return NF_DROP;
91
92         ct = ip_conntrack_get(*pskb, &ctinfo);
93         /* Can't track?  It's not due to stress, or conntrack would
94            have dropped it.  Hence it's the user's responsibilty to
95            packet filter it out, or implement conntrack/NAT for that
96            protocol. 8) --RR */
97         if (!ct) {
98                 /* Exception: ICMP redirect to new connection (not in
99                    hash table yet).  We must not let this through, in
100                    case we're doing NAT to the same network. */
101                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
102                         struct icmphdr _hdr, *hp;
103
104                         hp = skb_header_pointer(*pskb,
105                                                 (*pskb)->nh.iph->ihl*4,
106                                                 sizeof(_hdr), &_hdr);
107                         if (hp != NULL &&
108                             hp->type == ICMP_REDIRECT)
109                                 return NF_DROP;
110                 }
111                 return NF_ACCEPT;
112         }
113
114         switch (ctinfo) {
115         case IP_CT_RELATED:
116         case IP_CT_RELATED+IP_CT_IS_REPLY:
117                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
118                         if (!icmp_reply_translation(pskb, ct, hooknum,
119                                                     CTINFO2DIR(ctinfo)))
120                                 return NF_DROP;
121                         else
122                                 return NF_ACCEPT;
123                 }
124                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
125         case IP_CT_NEW:
126                 info = &ct->nat.info;
127
128                 WRITE_LOCK(&ip_nat_lock);
129                 /* Seen it before?  This can happen for loopback, retrans,
130                    or local packets.. */
131                 if (!(info->initialized & (1 << maniptype))
132 #ifndef CONFIG_IP_NF_NAT_LOCAL
133                     /* If this session has already been confirmed we must not
134                      * touch it again even if there is no mapping set up.
135                      * Can only happen on local->local traffic with
136                      * CONFIG_IP_NF_NAT_LOCAL disabled.
137                      */
138                     && !(ct->status & IPS_CONFIRMED)
139 #endif
140                     ) {
141                         unsigned int ret;
142
143                         if (ct->master
144                             && master_ct(ct)->nat.info.helper
145                             && master_ct(ct)->nat.info.helper->expect) {
146                                 ret = call_expect(master_ct(ct), pskb, 
147                                                   hooknum, ct, info);
148                         } else {
149 #ifdef CONFIG_IP_NF_NAT_LOCAL
150                                 /* LOCAL_IN hook doesn't have a chain!  */
151                                 if (hooknum == NF_IP_LOCAL_IN)
152                                         ret = alloc_null_binding(ct, info,
153                                                                  hooknum);
154                                 else
155 #endif
156                                 ret = ip_nat_rule_find(pskb, hooknum, in, out,
157                                                        ct, info);
158                         }
159
160                         if (ret != NF_ACCEPT) {
161                                 WRITE_UNLOCK(&ip_nat_lock);
162                                 return ret;
163                         }
164                 } else
165                         DEBUGP("Already setup manip %s for ct %p\n",
166                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
167                                ct);
168                 WRITE_UNLOCK(&ip_nat_lock);
169                 break;
170
171         default:
172                 /* ESTABLISHED */
173                 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
174                              || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
175                 info = &ct->nat.info;
176         }
177
178         IP_NF_ASSERT(info);
179         return do_bindings(ct, ctinfo, info, hooknum, pskb);
180 }
181
182 static unsigned int
183 ip_nat_out(unsigned int hooknum,
184            struct sk_buff **pskb,
185            const struct net_device *in,
186            const struct net_device *out,
187            int (*okfn)(struct sk_buff *))
188 {
189         /* root is playing with raw sockets. */
190         if ((*pskb)->len < sizeof(struct iphdr)
191             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
192                 return NF_ACCEPT;
193
194         /* We can hit fragment here; forwarded packets get
195            defragmented by connection tracking coming in, then
196            fragmented (grr) by the forward code.
197
198            In future: If we have nfct != NULL, AND we have NAT
199            initialized, AND there is no helper, then we can do full
200            NAPT on the head, and IP-address-only NAT on the rest.
201
202            I'm starting to have nightmares about fragments.  */
203
204         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
205                 *pskb = ip_ct_gather_frags(*pskb);
206
207                 if (!*pskb)
208                         return NF_STOLEN;
209         }
210
211         return ip_nat_fn(hooknum, pskb, in, out, okfn);
212 }
213
214 #ifdef CONFIG_IP_NF_NAT_LOCAL
215 static unsigned int
216 ip_nat_local_fn(unsigned int hooknum,
217                 struct sk_buff **pskb,
218                 const struct net_device *in,
219                 const struct net_device *out,
220                 int (*okfn)(struct sk_buff *))
221 {
222         u_int32_t saddr, daddr;
223         unsigned int ret;
224
225         /* root is playing with raw sockets. */
226         if ((*pskb)->len < sizeof(struct iphdr)
227             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
228                 return NF_ACCEPT;
229
230         saddr = (*pskb)->nh.iph->saddr;
231         daddr = (*pskb)->nh.iph->daddr;
232
233         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
234         if (ret != NF_DROP && ret != NF_STOLEN
235             && ((*pskb)->nh.iph->saddr != saddr
236                 || (*pskb)->nh.iph->daddr != daddr))
237                 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
238         return ret;
239 }
240 #endif
241
242 /* We must be after connection tracking and before packet filtering. */
243
244 /* Before packet filtering, change destination */
245 static struct nf_hook_ops ip_nat_in_ops = {
246         .hook           = ip_nat_fn,
247         .owner          = THIS_MODULE,
248         .pf             = PF_INET,
249         .hooknum        = NF_IP_PRE_ROUTING,
250         .priority       = NF_IP_PRI_NAT_DST,
251 };
252
253 /* After packet filtering, change source */
254 static struct nf_hook_ops ip_nat_out_ops = {
255         .hook           = ip_nat_out,
256         .owner          = THIS_MODULE,
257         .pf             = PF_INET,
258         .hooknum        = NF_IP_POST_ROUTING,
259         .priority       = NF_IP_PRI_NAT_SRC,
260 };
261
262 #ifdef CONFIG_IP_NF_NAT_LOCAL
263 /* Before packet filtering, change destination */
264 static struct nf_hook_ops ip_nat_local_out_ops = {
265         .hook           = ip_nat_local_fn,
266         .owner          = THIS_MODULE,
267         .pf             = PF_INET,
268         .hooknum        = NF_IP_LOCAL_OUT,
269         .priority       = NF_IP_PRI_NAT_DST,
270 };
271
272 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
273 static struct nf_hook_ops ip_nat_local_in_ops = {
274         .hook           = ip_nat_fn,
275         .owner          = THIS_MODULE,
276         .pf             = PF_INET,
277         .hooknum        = NF_IP_LOCAL_IN,
278         .priority       = NF_IP_PRI_NAT_SRC,
279 };
280 #endif
281
282 /* Protocol registration. */
283 int ip_nat_protocol_register(struct ip_nat_protocol *proto)
284 {
285         int ret = 0;
286
287         WRITE_LOCK(&ip_nat_lock);
288         if (ip_nat_protos[proto->protonum] != &ip_nat_unknown_protocol) {
289                 ret = -EBUSY;
290                 goto out;
291         }
292         ip_nat_protos[proto->protonum] = proto;
293  out:
294         WRITE_UNLOCK(&ip_nat_lock);
295         return ret;
296 }
297
298 /* Noone stores the protocol anywhere; simply delete it. */
299 void ip_nat_protocol_unregister(struct ip_nat_protocol *proto)
300 {
301         WRITE_LOCK(&ip_nat_lock);
302         ip_nat_protos[proto->protonum] = &ip_nat_unknown_protocol;
303         WRITE_UNLOCK(&ip_nat_lock);
304
305         /* Someone could be still looking at the proto in a bh. */
306         synchronize_net();
307 }
308
309 static int init_or_cleanup(int init)
310 {
311         int ret = 0;
312
313         need_ip_conntrack();
314
315         if (!init) goto cleanup;
316
317         ret = ip_nat_rule_init();
318         if (ret < 0) {
319                 printk("ip_nat_init: can't setup rules.\n");
320                 goto cleanup_nothing;
321         }
322         ret = ip_nat_init();
323         if (ret < 0) {
324                 printk("ip_nat_init: can't setup rules.\n");
325                 goto cleanup_rule_init;
326         }
327         ret = nf_register_hook(&ip_nat_in_ops);
328         if (ret < 0) {
329                 printk("ip_nat_init: can't register in hook.\n");
330                 goto cleanup_nat;
331         }
332         ret = nf_register_hook(&ip_nat_out_ops);
333         if (ret < 0) {
334                 printk("ip_nat_init: can't register out hook.\n");
335                 goto cleanup_inops;
336         }
337 #ifdef CONFIG_IP_NF_NAT_LOCAL
338         ret = nf_register_hook(&ip_nat_local_out_ops);
339         if (ret < 0) {
340                 printk("ip_nat_init: can't register local out hook.\n");
341                 goto cleanup_outops;
342         }
343         ret = nf_register_hook(&ip_nat_local_in_ops);
344         if (ret < 0) {
345                 printk("ip_nat_init: can't register local in hook.\n");
346                 goto cleanup_localoutops;
347         }
348 #endif
349         return ret;
350
351  cleanup:
352 #ifdef CONFIG_IP_NF_NAT_LOCAL
353         nf_unregister_hook(&ip_nat_local_in_ops);
354  cleanup_localoutops:
355         nf_unregister_hook(&ip_nat_local_out_ops);
356  cleanup_outops:
357 #endif
358         nf_unregister_hook(&ip_nat_out_ops);
359  cleanup_inops:
360         nf_unregister_hook(&ip_nat_in_ops);
361  cleanup_nat:
362         ip_nat_cleanup();
363  cleanup_rule_init:
364         ip_nat_rule_cleanup();
365  cleanup_nothing:
366         MUST_BE_READ_WRITE_UNLOCKED(&ip_nat_lock);
367         return ret;
368 }
369
370 static int __init init(void)
371 {
372         return init_or_cleanup(1);
373 }
374
375 static void __exit fini(void)
376 {
377         init_or_cleanup(0);
378 }
379
380 module_init(init);
381 module_exit(fini);
382
383 EXPORT_SYMBOL(ip_nat_setup_info);
384 EXPORT_SYMBOL(ip_nat_protocol_register);
385 EXPORT_SYMBOL(ip_nat_protocol_unregister);
386 EXPORT_SYMBOL(ip_nat_helper_register);
387 EXPORT_SYMBOL(ip_nat_helper_unregister);
388 EXPORT_SYMBOL(ip_nat_cheat_check);
389 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
390 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
391 EXPORT_SYMBOL(ip_nat_used_tuple);
392 EXPORT_SYMBOL(ip_nat_find_helper);
393 EXPORT_SYMBOL(__ip_nat_find_helper);
394 MODULE_LICENSE("GPL");