]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/netfilter/ip_nat_standalone.c
[PATCH] don't try to do any NAT on untracked connections
[linux-2.6-omap-h63xx.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)
35 #define ASSERT_WRITE_LOCK(x)
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 unsigned int
59 ip_nat_fn(unsigned int hooknum,
60           struct sk_buff **pskb,
61           const struct net_device *in,
62           const struct net_device *out,
63           int (*okfn)(struct sk_buff *))
64 {
65         struct ip_conntrack *ct;
66         enum ip_conntrack_info ctinfo;
67         struct ip_nat_info *info;
68         /* maniptype == SRC for postrouting. */
69         enum ip_nat_manip_type maniptype = HOOK2MANIP(hooknum);
70
71         /* We never see fragments: conntrack defrags on pre-routing
72            and local-out, and ip_nat_out protects post-routing. */
73         IP_NF_ASSERT(!((*pskb)->nh.iph->frag_off
74                        & htons(IP_MF|IP_OFFSET)));
75
76         (*pskb)->nfcache |= NFC_UNKNOWN;
77
78         /* If we had a hardware checksum before, it's now invalid */
79         if ((*pskb)->ip_summed == CHECKSUM_HW)
80                 if (skb_checksum_help(*pskb, (out == NULL)))
81                         return NF_DROP;
82
83         ct = ip_conntrack_get(*pskb, &ctinfo);
84         /* Can't track?  It's not due to stress, or conntrack would
85            have dropped it.  Hence it's the user's responsibilty to
86            packet filter it out, or implement conntrack/NAT for that
87            protocol. 8) --RR */
88         if (!ct) {
89                 /* Exception: ICMP redirect to new connection (not in
90                    hash table yet).  We must not let this through, in
91                    case we're doing NAT to the same network. */
92                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
93                         struct icmphdr _hdr, *hp;
94
95                         hp = skb_header_pointer(*pskb,
96                                                 (*pskb)->nh.iph->ihl*4,
97                                                 sizeof(_hdr), &_hdr);
98                         if (hp != NULL &&
99                             hp->type == ICMP_REDIRECT)
100                                 return NF_DROP;
101                 }
102                 return NF_ACCEPT;
103         }
104
105         /* Don't try to NAT if this packet is not conntracked */
106         if (ct == &ip_conntrack_untracked)
107                 return NF_ACCEPT;
108
109         switch (ctinfo) {
110         case IP_CT_RELATED:
111         case IP_CT_RELATED+IP_CT_IS_REPLY:
112                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
113                         if (!icmp_reply_translation(pskb, ct, maniptype,
114                                                     CTINFO2DIR(ctinfo)))
115                                 return NF_DROP;
116                         else
117                                 return NF_ACCEPT;
118                 }
119                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
120         case IP_CT_NEW:
121                 info = &ct->nat.info;
122
123                 /* Seen it before?  This can happen for loopback, retrans,
124                    or local packets.. */
125                 if (!ip_nat_initialized(ct, maniptype)) {
126                         unsigned int ret;
127
128                         /* LOCAL_IN hook doesn't have a chain!  */
129                         if (hooknum == NF_IP_LOCAL_IN)
130                                 ret = alloc_null_binding(ct, info, hooknum);
131                         else
132                                 ret = ip_nat_rule_find(pskb, hooknum,
133                                                        in, out, ct,
134                                                        info);
135
136                         if (ret != NF_ACCEPT) {
137                                 return ret;
138                         }
139                 } else
140                         DEBUGP("Already setup manip %s for ct %p\n",
141                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
142                                ct);
143                 break;
144
145         default:
146                 /* ESTABLISHED */
147                 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
148                              || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
149                 info = &ct->nat.info;
150         }
151
152         IP_NF_ASSERT(info);
153         return nat_packet(ct, ctinfo, hooknum, pskb);
154 }
155
156 static unsigned int
157 ip_nat_in(unsigned int hooknum,
158           struct sk_buff **pskb,
159           const struct net_device *in,
160           const struct net_device *out,
161           int (*okfn)(struct sk_buff *))
162 {
163         u_int32_t saddr, daddr;
164         unsigned int ret;
165
166         saddr = (*pskb)->nh.iph->saddr;
167         daddr = (*pskb)->nh.iph->daddr;
168
169         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
170         if (ret != NF_DROP && ret != NF_STOLEN
171             && ((*pskb)->nh.iph->saddr != saddr
172                 || (*pskb)->nh.iph->daddr != daddr)) {
173                 dst_release((*pskb)->dst);
174                 (*pskb)->dst = NULL;
175         }
176         return ret;
177 }
178
179 static unsigned int
180 ip_nat_out(unsigned int hooknum,
181            struct sk_buff **pskb,
182            const struct net_device *in,
183            const struct net_device *out,
184            int (*okfn)(struct sk_buff *))
185 {
186         /* root is playing with raw sockets. */
187         if ((*pskb)->len < sizeof(struct iphdr)
188             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
189                 return NF_ACCEPT;
190
191         /* We can hit fragment here; forwarded packets get
192            defragmented by connection tracking coming in, then
193            fragmented (grr) by the forward code.
194
195            In future: If we have nfct != NULL, AND we have NAT
196            initialized, AND there is no helper, then we can do full
197            NAPT on the head, and IP-address-only NAT on the rest.
198
199            I'm starting to have nightmares about fragments.  */
200
201         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
202                 *pskb = ip_ct_gather_frags(*pskb, IP_DEFRAG_NAT_OUT);
203
204                 if (!*pskb)
205                         return NF_STOLEN;
206         }
207
208         return ip_nat_fn(hooknum, pskb, in, out, okfn);
209 }
210
211 static unsigned int
212 ip_nat_local_fn(unsigned int hooknum,
213                 struct sk_buff **pskb,
214                 const struct net_device *in,
215                 const struct net_device *out,
216                 int (*okfn)(struct sk_buff *))
217 {
218         u_int32_t saddr, daddr;
219         unsigned int ret;
220
221         /* root is playing with raw sockets. */
222         if ((*pskb)->len < sizeof(struct iphdr)
223             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
224                 return NF_ACCEPT;
225
226         saddr = (*pskb)->nh.iph->saddr;
227         daddr = (*pskb)->nh.iph->daddr;
228
229         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
230         if (ret != NF_DROP && ret != NF_STOLEN
231             && ((*pskb)->nh.iph->saddr != saddr
232                 || (*pskb)->nh.iph->daddr != daddr))
233                 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
234         return ret;
235 }
236
237 static unsigned int
238 ip_nat_adjust(unsigned int hooknum,
239               struct sk_buff **pskb,
240               const struct net_device *in,
241               const struct net_device *out,
242               int (*okfn)(struct sk_buff *))
243 {
244         struct ip_conntrack *ct;
245         enum ip_conntrack_info ctinfo;
246
247         ct = ip_conntrack_get(*pskb, &ctinfo);
248         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
249                 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
250                 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
251                         return NF_DROP;
252         }
253         return NF_ACCEPT;
254 }
255
256 /* We must be after connection tracking and before packet filtering. */
257
258 /* Before packet filtering, change destination */
259 static struct nf_hook_ops ip_nat_in_ops = {
260         .hook           = ip_nat_in,
261         .owner          = THIS_MODULE,
262         .pf             = PF_INET,
263         .hooknum        = NF_IP_PRE_ROUTING,
264         .priority       = NF_IP_PRI_NAT_DST,
265 };
266
267 /* After packet filtering, change source */
268 static struct nf_hook_ops ip_nat_out_ops = {
269         .hook           = ip_nat_out,
270         .owner          = THIS_MODULE,
271         .pf             = PF_INET,
272         .hooknum        = NF_IP_POST_ROUTING,
273         .priority       = NF_IP_PRI_NAT_SRC,
274 };
275
276 /* After conntrack, adjust sequence number */
277 static struct nf_hook_ops ip_nat_adjust_out_ops = {
278         .hook           = ip_nat_adjust,
279         .owner          = THIS_MODULE,
280         .pf             = PF_INET,
281         .hooknum        = NF_IP_POST_ROUTING,
282         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
283 };
284
285 /* Before packet filtering, change destination */
286 static struct nf_hook_ops ip_nat_local_out_ops = {
287         .hook           = ip_nat_local_fn,
288         .owner          = THIS_MODULE,
289         .pf             = PF_INET,
290         .hooknum        = NF_IP_LOCAL_OUT,
291         .priority       = NF_IP_PRI_NAT_DST,
292 };
293
294 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
295 static struct nf_hook_ops ip_nat_local_in_ops = {
296         .hook           = ip_nat_fn,
297         .owner          = THIS_MODULE,
298         .pf             = PF_INET,
299         .hooknum        = NF_IP_LOCAL_IN,
300         .priority       = NF_IP_PRI_NAT_SRC,
301 };
302
303 /* After conntrack, adjust sequence number */
304 static struct nf_hook_ops ip_nat_adjust_in_ops = {
305         .hook           = ip_nat_adjust,
306         .owner          = THIS_MODULE,
307         .pf             = PF_INET,
308         .hooknum        = NF_IP_LOCAL_IN,
309         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
310 };
311
312
313 static int init_or_cleanup(int init)
314 {
315         int ret = 0;
316
317         need_ip_conntrack();
318
319         if (!init) goto cleanup;
320
321         ret = ip_nat_rule_init();
322         if (ret < 0) {
323                 printk("ip_nat_init: can't setup rules.\n");
324                 goto cleanup_nothing;
325         }
326         ret = ip_nat_init();
327         if (ret < 0) {
328                 printk("ip_nat_init: can't setup rules.\n");
329                 goto cleanup_rule_init;
330         }
331         ret = nf_register_hook(&ip_nat_in_ops);
332         if (ret < 0) {
333                 printk("ip_nat_init: can't register in hook.\n");
334                 goto cleanup_nat;
335         }
336         ret = nf_register_hook(&ip_nat_out_ops);
337         if (ret < 0) {
338                 printk("ip_nat_init: can't register out hook.\n");
339                 goto cleanup_inops;
340         }
341         ret = nf_register_hook(&ip_nat_adjust_in_ops);
342         if (ret < 0) {
343                 printk("ip_nat_init: can't register adjust in hook.\n");
344                 goto cleanup_outops;
345         }
346         ret = nf_register_hook(&ip_nat_adjust_out_ops);
347         if (ret < 0) {
348                 printk("ip_nat_init: can't register adjust out hook.\n");
349                 goto cleanup_adjustin_ops;
350         }
351         ret = nf_register_hook(&ip_nat_local_out_ops);
352         if (ret < 0) {
353                 printk("ip_nat_init: can't register local out hook.\n");
354                 goto cleanup_adjustout_ops;;
355         }
356         ret = nf_register_hook(&ip_nat_local_in_ops);
357         if (ret < 0) {
358                 printk("ip_nat_init: can't register local in hook.\n");
359                 goto cleanup_localoutops;
360         }
361         return ret;
362
363  cleanup:
364         nf_unregister_hook(&ip_nat_local_in_ops);
365  cleanup_localoutops:
366         nf_unregister_hook(&ip_nat_local_out_ops);
367  cleanup_adjustout_ops:
368         nf_unregister_hook(&ip_nat_adjust_out_ops);
369  cleanup_adjustin_ops:
370         nf_unregister_hook(&ip_nat_adjust_in_ops);
371  cleanup_outops:
372         nf_unregister_hook(&ip_nat_out_ops);
373  cleanup_inops:
374         nf_unregister_hook(&ip_nat_in_ops);
375  cleanup_nat:
376         ip_nat_cleanup();
377  cleanup_rule_init:
378         ip_nat_rule_cleanup();
379  cleanup_nothing:
380         return ret;
381 }
382
383 static int __init init(void)
384 {
385         return init_or_cleanup(1);
386 }
387
388 static void __exit fini(void)
389 {
390         init_or_cleanup(0);
391 }
392
393 module_init(init);
394 module_exit(fini);
395
396 EXPORT_SYMBOL(ip_nat_setup_info);
397 EXPORT_SYMBOL(ip_nat_protocol_register);
398 EXPORT_SYMBOL(ip_nat_protocol_unregister);
399 EXPORT_SYMBOL(ip_nat_cheat_check);
400 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
401 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
402 EXPORT_SYMBOL(ip_nat_used_tuple);
403 EXPORT_SYMBOL(ip_nat_follow_master);
404 MODULE_LICENSE("GPL");