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