bc59d0d6e89ef5bf16512ea6cc0a5d246d6a4f2d
[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)
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         switch (ctinfo) {
106         case IP_CT_RELATED:
107         case IP_CT_RELATED+IP_CT_IS_REPLY:
108                 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP) {
109                         if (!icmp_reply_translation(pskb, ct, maniptype,
110                                                     CTINFO2DIR(ctinfo)))
111                                 return NF_DROP;
112                         else
113                                 return NF_ACCEPT;
114                 }
115                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
116         case IP_CT_NEW:
117                 info = &ct->nat.info;
118
119                 /* Seen it before?  This can happen for loopback, retrans,
120                    or local packets.. */
121                 if (!ip_nat_initialized(ct, maniptype)) {
122                         unsigned int ret;
123
124                         /* LOCAL_IN hook doesn't have a chain!  */
125                         if (hooknum == NF_IP_LOCAL_IN)
126                                 ret = alloc_null_binding(ct, info, hooknum);
127                         else
128                                 ret = ip_nat_rule_find(pskb, hooknum,
129                                                        in, out, ct,
130                                                        info);
131
132                         if (ret != NF_ACCEPT) {
133                                 return ret;
134                         }
135                 } else
136                         DEBUGP("Already setup manip %s for ct %p\n",
137                                maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST",
138                                ct);
139                 break;
140
141         default:
142                 /* ESTABLISHED */
143                 IP_NF_ASSERT(ctinfo == IP_CT_ESTABLISHED
144                              || ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY));
145                 info = &ct->nat.info;
146         }
147
148         IP_NF_ASSERT(info);
149         return nat_packet(ct, ctinfo, hooknum, pskb);
150 }
151
152 static unsigned int
153 ip_nat_in(unsigned int hooknum,
154           struct sk_buff **pskb,
155           const struct net_device *in,
156           const struct net_device *out,
157           int (*okfn)(struct sk_buff *))
158 {
159         u_int32_t saddr, daddr;
160         unsigned int ret;
161
162         saddr = (*pskb)->nh.iph->saddr;
163         daddr = (*pskb)->nh.iph->daddr;
164
165         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
166         if (ret != NF_DROP && ret != NF_STOLEN
167             && ((*pskb)->nh.iph->saddr != saddr
168                 || (*pskb)->nh.iph->daddr != daddr)) {
169                 dst_release((*pskb)->dst);
170                 (*pskb)->dst = NULL;
171         }
172         return ret;
173 }
174
175 static unsigned int
176 ip_nat_out(unsigned int hooknum,
177            struct sk_buff **pskb,
178            const struct net_device *in,
179            const struct net_device *out,
180            int (*okfn)(struct sk_buff *))
181 {
182         /* root is playing with raw sockets. */
183         if ((*pskb)->len < sizeof(struct iphdr)
184             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
185                 return NF_ACCEPT;
186
187         /* We can hit fragment here; forwarded packets get
188            defragmented by connection tracking coming in, then
189            fragmented (grr) by the forward code.
190
191            In future: If we have nfct != NULL, AND we have NAT
192            initialized, AND there is no helper, then we can do full
193            NAPT on the head, and IP-address-only NAT on the rest.
194
195            I'm starting to have nightmares about fragments.  */
196
197         if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
198                 *pskb = ip_ct_gather_frags(*pskb, IP_DEFRAG_NAT_OUT);
199
200                 if (!*pskb)
201                         return NF_STOLEN;
202         }
203
204         return ip_nat_fn(hooknum, pskb, in, out, okfn);
205 }
206
207 static unsigned int
208 ip_nat_local_fn(unsigned int hooknum,
209                 struct sk_buff **pskb,
210                 const struct net_device *in,
211                 const struct net_device *out,
212                 int (*okfn)(struct sk_buff *))
213 {
214         u_int32_t saddr, daddr;
215         unsigned int ret;
216
217         /* root is playing with raw sockets. */
218         if ((*pskb)->len < sizeof(struct iphdr)
219             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr))
220                 return NF_ACCEPT;
221
222         saddr = (*pskb)->nh.iph->saddr;
223         daddr = (*pskb)->nh.iph->daddr;
224
225         ret = ip_nat_fn(hooknum, pskb, in, out, okfn);
226         if (ret != NF_DROP && ret != NF_STOLEN
227             && ((*pskb)->nh.iph->saddr != saddr
228                 || (*pskb)->nh.iph->daddr != daddr))
229                 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
230         return ret;
231 }
232
233 static unsigned int
234 ip_nat_adjust(unsigned int hooknum,
235               struct sk_buff **pskb,
236               const struct net_device *in,
237               const struct net_device *out,
238               int (*okfn)(struct sk_buff *))
239 {
240         struct ip_conntrack *ct;
241         enum ip_conntrack_info ctinfo;
242
243         ct = ip_conntrack_get(*pskb, &ctinfo);
244         if (ct && test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
245                 DEBUGP("ip_nat_standalone: adjusting sequence number\n");
246                 if (!ip_nat_seq_adjust(pskb, ct, ctinfo))
247                         return NF_DROP;
248         }
249         return NF_ACCEPT;
250 }
251
252 /* We must be after connection tracking and before packet filtering. */
253
254 /* Before packet filtering, change destination */
255 static struct nf_hook_ops ip_nat_in_ops = {
256         .hook           = ip_nat_in,
257         .owner          = THIS_MODULE,
258         .pf             = PF_INET,
259         .hooknum        = NF_IP_PRE_ROUTING,
260         .priority       = NF_IP_PRI_NAT_DST,
261 };
262
263 /* After packet filtering, change source */
264 static struct nf_hook_ops ip_nat_out_ops = {
265         .hook           = ip_nat_out,
266         .owner          = THIS_MODULE,
267         .pf             = PF_INET,
268         .hooknum        = NF_IP_POST_ROUTING,
269         .priority       = NF_IP_PRI_NAT_SRC,
270 };
271
272 /* After conntrack, adjust sequence number */
273 static struct nf_hook_ops ip_nat_adjust_out_ops = {
274         .hook           = ip_nat_adjust,
275         .owner          = THIS_MODULE,
276         .pf             = PF_INET,
277         .hooknum        = NF_IP_POST_ROUTING,
278         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
279 };
280
281 /* Before packet filtering, change destination */
282 static struct nf_hook_ops ip_nat_local_out_ops = {
283         .hook           = ip_nat_local_fn,
284         .owner          = THIS_MODULE,
285         .pf             = PF_INET,
286         .hooknum        = NF_IP_LOCAL_OUT,
287         .priority       = NF_IP_PRI_NAT_DST,
288 };
289
290 /* After packet filtering, change source for reply packets of LOCAL_OUT DNAT */
291 static struct nf_hook_ops ip_nat_local_in_ops = {
292         .hook           = ip_nat_fn,
293         .owner          = THIS_MODULE,
294         .pf             = PF_INET,
295         .hooknum        = NF_IP_LOCAL_IN,
296         .priority       = NF_IP_PRI_NAT_SRC,
297 };
298
299 /* After conntrack, adjust sequence number */
300 static struct nf_hook_ops ip_nat_adjust_in_ops = {
301         .hook           = ip_nat_adjust,
302         .owner          = THIS_MODULE,
303         .pf             = PF_INET,
304         .hooknum        = NF_IP_LOCAL_IN,
305         .priority       = NF_IP_PRI_NAT_SEQ_ADJUST,
306 };
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         ret = nf_register_hook(&ip_nat_adjust_in_ops);
338         if (ret < 0) {
339                 printk("ip_nat_init: can't register adjust in hook.\n");
340                 goto cleanup_outops;
341         }
342         ret = nf_register_hook(&ip_nat_adjust_out_ops);
343         if (ret < 0) {
344                 printk("ip_nat_init: can't register adjust out hook.\n");
345                 goto cleanup_adjustin_ops;
346         }
347         ret = nf_register_hook(&ip_nat_local_out_ops);
348         if (ret < 0) {
349                 printk("ip_nat_init: can't register local out hook.\n");
350                 goto cleanup_adjustout_ops;;
351         }
352         ret = nf_register_hook(&ip_nat_local_in_ops);
353         if (ret < 0) {
354                 printk("ip_nat_init: can't register local in hook.\n");
355                 goto cleanup_localoutops;
356         }
357         return ret;
358
359  cleanup:
360         nf_unregister_hook(&ip_nat_local_in_ops);
361  cleanup_localoutops:
362         nf_unregister_hook(&ip_nat_local_out_ops);
363  cleanup_adjustout_ops:
364         nf_unregister_hook(&ip_nat_adjust_out_ops);
365  cleanup_adjustin_ops:
366         nf_unregister_hook(&ip_nat_adjust_in_ops);
367  cleanup_outops:
368         nf_unregister_hook(&ip_nat_out_ops);
369  cleanup_inops:
370         nf_unregister_hook(&ip_nat_in_ops);
371  cleanup_nat:
372         ip_nat_cleanup();
373  cleanup_rule_init:
374         ip_nat_rule_cleanup();
375  cleanup_nothing:
376         return ret;
377 }
378
379 static int __init init(void)
380 {
381         return init_or_cleanup(1);
382 }
383
384 static void __exit fini(void)
385 {
386         init_or_cleanup(0);
387 }
388
389 module_init(init);
390 module_exit(fini);
391
392 EXPORT_SYMBOL(ip_nat_setup_info);
393 EXPORT_SYMBOL(ip_nat_protocol_register);
394 EXPORT_SYMBOL(ip_nat_protocol_unregister);
395 EXPORT_SYMBOL(ip_nat_cheat_check);
396 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
397 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
398 EXPORT_SYMBOL(ip_nat_used_tuple);
399 EXPORT_SYMBOL(ip_nat_follow_master);
400 MODULE_LICENSE("GPL");