e1000: FIX: Stop raw interrupts disabled nag from RT
[pandora-kernel.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/types.h>
22 #include <linux/icmp.h>
23 #include <linux/ip.h>
24 #include <linux/netfilter.h>
25 #include <linux/netfilter_ipv4.h>
26 #include <linux/module.h>
27 #include <linux/skbuff.h>
28 #include <linux/proc_fs.h>
29 #include <net/ip.h>
30 #include <net/checksum.h>
31 #include <linux/spinlock.h>
32
33 #include <linux/netfilter_ipv4/ip_nat.h>
34 #include <linux/netfilter_ipv4/ip_nat_rule.h>
35 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
36 #include <linux/netfilter_ipv4/ip_nat_core.h>
37 #include <linux/netfilter_ipv4/ip_nat_helper.h>
38 #include <linux/netfilter_ipv4/ip_tables.h>
39 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
40
41 #if 0
42 #define DEBUGP printk
43 #else
44 #define DEBUGP(format, args...)
45 #endif
46
47 #ifdef CONFIG_XFRM
48 static void nat_decode_session(struct sk_buff *skb, struct flowi *fl)
49 {
50         struct ip_conntrack *ct;
51         struct ip_conntrack_tuple *t;
52         enum ip_conntrack_info ctinfo;
53         enum ip_conntrack_dir dir;
54         unsigned long statusbit;
55
56         ct = ip_conntrack_get(skb, &ctinfo);
57         if (ct == NULL)
58                 return;
59         dir = CTINFO2DIR(ctinfo);
60         t = &ct->tuplehash[dir].tuple;
61
62         if (dir == IP_CT_DIR_ORIGINAL)
63                 statusbit = IPS_DST_NAT;
64         else
65                 statusbit = IPS_SRC_NAT;
66
67         if (ct->status & statusbit) {
68                 fl->fl4_dst = t->dst.ip;
69                 if (t->dst.protonum == IPPROTO_TCP ||
70                     t->dst.protonum == IPPROTO_UDP)
71                         fl->fl_ip_dport = t->dst.u.tcp.port;
72         }
73
74         statusbit ^= IPS_NAT_MASK;
75
76         if (ct->status & statusbit) {
77                 fl->fl4_src = t->src.ip;
78                 if (t->dst.protonum == IPPROTO_TCP ||
79                     t->dst.protonum == IPPROTO_UDP)
80                         fl->fl_ip_sport = t->src.u.tcp.port;
81         }
82 }
83 #endif
84
85 static unsigned int
86 ip_nat_fn(unsigned int hooknum,
87           struct sk_buff **pskb,
88           const struct net_device *in,
89           const struct net_device *out,
90           int (*okfn)(struct sk_buff *))
91 {
92         struct ip_conntrack *ct;
93         enum ip_conntrack_info ctinfo;
94         struct ip_nat_info *info;
95         /* maniptype == SRC for postrouting. */
96         enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
97
98         /* We never see fragments: conntrack defrags on pre-routing
99            and local-out, and ip_nat_out protects post-routing. */
100         IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
101                        & htons(IP_MF|IP_OFFSET)));
102
103         ct = ip_conntrack_get(*pskb, &ctinfo);
104         /* Can't track?  It's not due to stress, or conntrack would
105            have dropped it.  Hence it's the user's responsibilty to
106            packet filter it out, or implement conntrack/NAT for that
107            protocol. 8) --RR */
108         if (!ct) {
109                 /* Exception: ICMP redirect to new connection (not in
110                    hash table yet).  We must not let this through, in
111                    case we're doing NAT to the same network. */
112                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
113                         struct icmphdr _hdr, *hp;
114
115                         hp = skb_header_pointer(*pskb,
116                                                 (*pskb)->nh.iph->ihl*4,
117                                                 sizeof(_hdr), &_hdr);
118                         if (hp != NULL &&
119                             hp->type == ICMP_REDIRECT)
120                                 return NF_DROP;
121                 }
122                 return NF_ACCEPT;
123         }
124
125         /* Don't try to NAT if this packet is not conntracked */
126         if (ct == &ip_conntrack_untracked)
127                 return NF_ACCEPT;
128
129         switch (ctinfo) {
130         case IP_CT_RELATED:
131         case IP_CT_RELATED+IP_CT_IS_REPLY:
132                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
133                         if (!ip_nat_icmp_reply_translation(ct, ctinfo,
134                                                            hooknum, pskb))
135                                 return NF_DROP;
136                         else
137                                 return NF_ACCEPT;
138                 }
139                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
140         case IP_CT_NEW:
141                 info = &ct->nat.info;
142
143                 /* Seen it before?  This can happen for loopback, retrans,
144                    or local packets.. */
145                 if (!ip_nat_initialized(ct, maniptype)) {
146                         unsigned int ret;
147
148                         if (unlikely(is_confirmed(ct)))
149                                 /* NAT module was loaded late */
150                                 ret = alloc_null_binding_confirmed(ct, info,
151                                                                    hooknum);
152                         else if (hooknum == NF_IP_LOCAL_IN)
153                                 /* LOCAL_IN hook doesn't have a chain!  */
154                                 ret = alloc_null_binding(ct, info, hooknum);
155                         else
156                                 ret = ip_nat_rule_find(pskb, hooknum,
157                                                        in, out, ct,
158                                                        info);
159
160                         if (ret != NF_ACCEPT) {
161                                 return ret;
162                         }
163                 } else
164                         DEBUGP("Already setup manip %s for ct %p\n",
165                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
166                                ct);
167                 break;
168
169         default:
170                 /* ESTABLISHED */
171                 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
172                              || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
173                 info = &ct->nat.info;
174         }
175
176         IP_NF_ASSERT(info);
177         return ip_nat_packet(ct, ctinfo, hooknum, pskb);
178 }
179
180 static unsigned int
181 ip_nat_in(unsigned int hooknum,
182           struct sk_buff **pskb,
183           const struct net_device *in,
184           const struct net_device *out,
185           int (*okfn)(struct sk_buff *))
186 {
187         unsigned int ret;
188         __be32 daddr = (*pskb)->nh.iph->daddr;
189
190         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
191         if (ret != NF_DROP && ret != NF_STOLEN
192             && daddr != (*pskb)->nh.iph->daddr) {
193                 dst_release((*pskb)->dst);
194                 (*pskb)->dst = NULL;
195         }
196         return ret;
197 }
198
199 static unsigned int
200 ip_nat_out(unsigned int hooknum,
201            struct sk_buff **pskb,
202            const struct net_device *in,
203            const struct net_device *out,
204            int (*okfn)(struct sk_buff *))
205 {
206 #ifdef CONFIG_XFRM
207         struct ip_conntrack *ct;
208         enum ip_conntrack_info ctinfo;
209 #endif
210         unsigned int ret;
211
212         /* root is playing with raw sockets. */
213         if ((*pskb)->len < sizeof(struct iphdr)
214             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
215                 return NF_ACCEPT;
216
217         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
218 #ifdef CONFIG_XFRM
219         if (ret != NF_DROP && ret != NF_STOLEN
220             && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
221                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
222
223                 if (ct->tuplehash[dir].tuple.src.ip !=
224                     ct->tuplehash[!dir].tuple.dst.ip
225                     || ct->tuplehash[dir].tuple.src.u.all !=
226                        ct->tuplehash[!dir].tuple.dst.u.all
227                     )
228                         return ip_xfrm_me_harder(pskb) == 0 ? ret : NF_DROP;
229         }
230 #endif
231         return ret;
232 }
233
234 static unsigned int
235 ip_nat_local_fn(unsigned int hooknum,
236                 struct sk_buff **pskb,
237                 const struct net_device *in,
238                 const struct net_device *out,
239                 int (*okfn)(struct sk_buff *))
240 {
241         struct ip_conntrack *ct;
242         enum ip_conntrack_info ctinfo;
243         unsigned int ret;
244
245         /* root is playing with raw sockets. */
246         if ((*pskb)->len < sizeof(struct iphdr)
247             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
248                 return NF_ACCEPT;
249
250         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
251         if (ret != NF_DROP && ret != NF_STOLEN
252             && (ct = ip_conntrack_get(*pskb, &ctinfo)) != NULL) {
253                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
254
255                 if (ct->tuplehash[dir].tuple.dst.ip !=
256                     ct->tuplehash[!dir].tuple.src.ip) {
257                         if (ip_route_me_harder(pskb, RTN_UNSPEC))
258                                 ret = NF_DROP;
259                 }
260 #ifdef CONFIG_XFRM
261                 else if (ct->tuplehash[dir].tuple.dst.u.all !=
262                          ct->tuplehash[!dir].tuple.src.u.all)
263                         if (ip_xfrm_me_harder(pskb))
264                                 ret = NF_DROP;
265 #endif
266
267         }
268         return ret;
269 }
270
271 static unsigned int
272 ip_nat_adjust(unsigned int hooknum,
273               struct sk_buff **pskb,
274               const struct net_device *in,
275               const struct net_device *out,
276               int (*okfn)(struct sk_buff *))
277 {
278         struct ip_conntrack *ct;
279         enum ip_conntrack_info ctinfo;
280
281         ct = ip_conntrack_get(*pskb, &ctinfo);
282         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
283                 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
284                 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
285                         return NF_DROP;
286         }
287         return NF_ACCEPT;
288 }
289
290 /* We must be after connection tracking and before packet filtering. */
291
292 static struct nf_hook_ops ip_nat_ops[] = {
293         /* Before packet filtering, change destination */
294         {
295                 .hook           = ip_nat_in,
296                 .owner          = THIS_MODULE,
297                 .pf             = PF_INET,
298                 .hooknum        = NF_IP_PRE_ROUTING,
299                 .priority       = NF_IP_PRI_NAT_DST,
300         },
301         /* After packet filtering, change source */
302         {
303                 .hook           = ip_nat_out,
304                 .owner          = THIS_MODULE,
305                 .pf             = PF_INET,
306                 .hooknum        = NF_IP_POST_ROUTING,
307                 .priority       = NF_IP_PRI_NAT_SRC,
308         },
309         /* After conntrack, adjust sequence number */
310         {
311                 .hook           = ip_nat_adjust,
312                 .owner          = THIS_MODULE,
313                 .pf             = PF_INET,
314                 .hooknum        = NF_IP_POST_ROUTING,
315                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
316         },
317         /* Before packet filtering, change destination */
318         {
319                 .hook           = ip_nat_local_fn,
320                 .owner          = THIS_MODULE,
321                 .pf             = PF_INET,
322                 .hooknum        = NF_IP_LOCAL_OUT,
323                 .priority       = NF_IP_PRI_NAT_DST,
324         },
325         /* After packet filtering, change source */
326         {
327                 .hook           = ip_nat_fn,
328                 .owner          = THIS_MODULE,
329                 .pf             = PF_INET,
330                 .hooknum        = NF_IP_LOCAL_IN,
331                 .priority       = NF_IP_PRI_NAT_SRC,
332         },
333         /* After conntrack, adjust sequence number */
334         {
335                 .hook           = ip_nat_adjust,
336                 .owner          = THIS_MODULE,
337                 .pf             = PF_INET,
338                 .hooknum        = NF_IP_LOCAL_IN,
339                 .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
340         },
341 };
342
343 static int __init ip_nat_standalone_init(void)
344 {
345         int ret = 0;
346
347         need_conntrack();
348
349 #ifdef CONFIG_XFRM
350         BUG_ON(ip_nat_decode_session != NULL);
351         ip_nat_decode_session = nat_decode_session;
352 #endif
353         ret = ip_nat_rule_init();
354         if (ret < 0) {
355                 printk("ip_nat_init: can't setup rules.\n");
356                 goto cleanup_decode_session;
357         }
358         ret = nf_register_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
359         if (ret < 0) {
360                 printk("ip_nat_init: can't register hooks.\n");
361                 goto cleanup_rule_init;
362         }
363         return ret;
364
365  cleanup_rule_init:
366         ip_nat_rule_cleanup();
367  cleanup_decode_session:
368 #ifdef CONFIG_XFRM
369         ip_nat_decode_session = NULL;
370         synchronize_net();
371 #endif
372         return ret;
373 }
374
375 static void __exit ip_nat_standalone_fini(void)
376 {
377         nf_unregister_hooks(ip_nat_ops, ARRAY_SIZE(ip_nat_ops));
378         ip_nat_rule_cleanup();
379 #ifdef CONFIG_XFRM
380         ip_nat_decode_session = NULL;
381         synchronize_net();
382 #endif
383 }
384
385 module_init(ip_nat_standalone_init);
386 module_exit(ip_nat_standalone_fini);
387
388 MODULE_LICENSE("GPL");