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