Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / net / ipv4 / netfilter / ipt_NETMAP.c
1 /* NETMAP - static NAT mapping of IP network addresses (1:1).
2  * The mapping can be applied to source (POSTROUTING),
3  * destination (PREROUTING), or both (with separate rules).
4  */
5
6 /* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/ip.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/netfilter_ipv4/ip_nat_rule.h>
19
20 #define MODULENAME "NETMAP"
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
23 MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target");
24
25 #if 0
26 #define DEBUGP printk
27 #else
28 #define DEBUGP(format, args...)
29 #endif
30
31 static int
32 check(const char *tablename,
33       const void *e,
34       const struct xt_target *target,
35       void *targinfo,
36       unsigned int targinfosize,
37       unsigned int hook_mask)
38 {
39         const struct ip_nat_multi_range_compat *mr = targinfo;
40
41         if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
42                 DEBUGP(MODULENAME":check: bad MAP_IPS.\n");
43                 return 0;
44         }
45         if (mr->rangesize != 1) {
46                 DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize);
47                 return 0;
48         }
49         return 1;
50 }
51
52 static unsigned int
53 target(struct sk_buff **pskb,
54        const struct net_device *in,
55        const struct net_device *out,
56        unsigned int hooknum,
57        const struct xt_target *target,
58        const void *targinfo,
59        void *userinfo)
60 {
61         struct ip_conntrack *ct;
62         enum ip_conntrack_info ctinfo;
63         u_int32_t new_ip, netmask;
64         const struct ip_nat_multi_range_compat *mr = targinfo;
65         struct ip_nat_range newrange;
66
67         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
68                      || hooknum == NF_IP_POST_ROUTING
69                      || hooknum == NF_IP_LOCAL_OUT);
70         ct = ip_conntrack_get(*pskb, &ctinfo);
71
72         netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
73
74         if (hooknum == NF_IP_PRE_ROUTING || hooknum == NF_IP_LOCAL_OUT)
75                 new_ip = (*pskb)->nh.iph->daddr & ~netmask;
76         else
77                 new_ip = (*pskb)->nh.iph->saddr & ~netmask;
78         new_ip |= mr->range[0].min_ip & netmask;
79
80         newrange = ((struct ip_nat_range)
81                 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
82                   new_ip, new_ip,
83                   mr->range[0].min, mr->range[0].max });
84
85         /* Hand modified range to generic setup. */
86         return ip_nat_setup_info(ct, &newrange, hooknum);
87 }
88
89 static struct ipt_target target_module = { 
90         .name           = MODULENAME,
91         .target         = target, 
92         .targetsize     = sizeof(struct ip_nat_multi_range_compat),
93         .table          = "nat",
94         .hooks          = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING) |
95                           (1 << NF_IP_LOCAL_OUT),
96         .checkentry     = check,
97         .me             = THIS_MODULE 
98 };
99
100 static int __init ipt_netmap_init(void)
101 {
102         return ipt_register_target(&target_module);
103 }
104
105 static void __exit ipt_netmap_fini(void)
106 {
107         ipt_unregister_target(&target_module);
108 }
109
110 module_init(ipt_netmap_init);
111 module_exit(ipt_netmap_fini);