fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / net / ipv4 / netfilter / ipt_SAME.c
1 /* Same.  Just like SNAT, only try to make the connections
2  *        between client A and server B always have the same source ip.
3  *
4  * (C) 2000 Paul `Rusty' Russell
5  * (C) 2001 Martin Josefsson
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * 010320 Martin Josefsson <gandalf@wlug.westbo.se>
12  *      * copied ipt_BALANCE.c to ipt_SAME.c and changed a few things.
13  * 010728 Martin Josefsson <gandalf@wlug.westbo.se>
14  *      * added --nodst to not include destination-ip in new source
15  *        calculations.
16  *      * added some more sanity-checks.
17  * 010729 Martin Josefsson <gandalf@wlug.westbo.se>
18  *      * fixed a buggy if-statement in same_check(), should have
19  *        used ntohl() but didn't.
20  *      * added support for multiple ranges. IPT_SAME_MAX_RANGE is
21  *        defined in linux/include/linux/netfilter_ipv4/ipt_SAME.h
22  *        and is currently set to 10.
23  *      * added support for 1-address range, nice to have now that
24  *        we have multiple ranges.
25  */
26 #include <linux/types.h>
27 #include <linux/ip.h>
28 #include <linux/timer.h>
29 #include <linux/module.h>
30 #include <linux/netfilter.h>
31 #include <linux/netdevice.h>
32 #include <linux/if.h>
33 #include <linux/inetdevice.h>
34 #include <net/protocol.h>
35 #include <net/checksum.h>
36 #include <linux/netfilter_ipv4.h>
37 #ifdef CONFIG_NF_NAT_NEEDED
38 #include <net/netfilter/nf_nat_rule.h>
39 #else
40 #include <linux/netfilter_ipv4/ip_nat_rule.h>
41 #endif
42 #include <linux/netfilter_ipv4/ipt_SAME.h>
43
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
46 MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");
47
48 #if 0
49 #define DEBUGP printk
50 #else
51 #define DEBUGP(format, args...)
52 #endif
53
54 static int
55 same_check(const char *tablename,
56               const void *e,
57               const struct xt_target *target,
58               void *targinfo,
59               unsigned int hook_mask)
60 {
61         unsigned int count, countess, rangeip, index = 0;
62         struct ipt_same_info *mr = targinfo;
63
64         mr->ipnum = 0;
65
66         if (mr->rangesize < 1) {
67                 DEBUGP("same_check: need at least one dest range.\n");
68                 return 0;
69         }
70         if (mr->rangesize > IPT_SAME_MAX_RANGE) {
71                 DEBUGP("same_check: too many ranges specified, maximum "
72                                 "is %u ranges\n",
73                                 IPT_SAME_MAX_RANGE);
74                 return 0;
75         }
76         for (count = 0; count < mr->rangesize; count++) {
77                 if (ntohl(mr->range[count].min_ip) >
78                                 ntohl(mr->range[count].max_ip)) {
79                         DEBUGP("same_check: min_ip is larger than max_ip in "
80                                 "range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
81                                 NIPQUAD(mr->range[count].min_ip),
82                                 NIPQUAD(mr->range[count].max_ip));
83                         return 0;
84                 }
85                 if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
86                         DEBUGP("same_check: bad MAP_IPS.\n");
87                         return 0;
88                 }
89                 rangeip = (ntohl(mr->range[count].max_ip) - 
90                                         ntohl(mr->range[count].min_ip) + 1);
91                 mr->ipnum += rangeip;
92                 
93                 DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
94         }
95         DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
96         
97         mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
98         if (!mr->iparray) {
99                 DEBUGP("same_check: Couldn't allocate %u bytes "
100                         "for %u ipaddresses!\n", 
101                         (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
102                 return 0;
103         }
104         DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
105                         (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
106         
107         for (count = 0; count < mr->rangesize; count++) {
108                 for (countess = ntohl(mr->range[count].min_ip);
109                                 countess <= ntohl(mr->range[count].max_ip);
110                                         countess++) {
111                         mr->iparray[index] = countess;
112                         DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
113                                 "in index %u.\n",
114                                 HIPQUAD(countess), index);
115                         index++;
116                 }
117         }
118         return 1;
119 }
120
121 static void 
122 same_destroy(const struct xt_target *target, void *targinfo)
123 {
124         struct ipt_same_info *mr = targinfo;
125
126         kfree(mr->iparray);
127         
128         DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
129                         (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
130 }
131
132 static unsigned int
133 same_target(struct sk_buff **pskb,
134                 const struct net_device *in,
135                 const struct net_device *out,
136                 unsigned int hooknum,
137                 const struct xt_target *target,
138                 const void *targinfo)
139 {
140         struct ip_conntrack *ct;
141         enum ip_conntrack_info ctinfo;
142         u_int32_t tmpip, aindex;
143         __be32 new_ip;
144         const struct ipt_same_info *same = targinfo;
145         struct ip_nat_range newrange;
146         const struct ip_conntrack_tuple *t;
147
148         IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
149                         hooknum == NF_IP_POST_ROUTING);
150         ct = ip_conntrack_get(*pskb, &ctinfo);
151
152         t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
153
154         /* Base new source on real src ip and optionally dst ip,
155            giving some hope for consistency across reboots.
156            Here we calculate the index in same->iparray which
157            holds the ipaddress we should use */
158         
159 #ifdef CONFIG_NF_NAT_NEEDED
160         tmpip = ntohl(t->src.u3.ip);
161
162         if (!(same->info & IPT_SAME_NODST))
163                 tmpip += ntohl(t->dst.u3.ip);
164 #else
165         tmpip = ntohl(t->src.ip);
166
167         if (!(same->info & IPT_SAME_NODST))
168                 tmpip += ntohl(t->dst.ip);
169 #endif
170         aindex = tmpip % same->ipnum;
171
172         new_ip = htonl(same->iparray[aindex]);
173
174         DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
175                         "new src=%u.%u.%u.%u\n",
176                         NIPQUAD(t->src.ip), NIPQUAD(t->dst.ip),
177                         NIPQUAD(new_ip));
178
179         /* Transfer from original range. */
180         newrange = ((struct ip_nat_range)
181                 { same->range[0].flags, new_ip, new_ip,
182                   /* FIXME: Use ports from correct range! */
183                   same->range[0].min, same->range[0].max });
184
185         /* Hand modified range to generic setup. */
186         return ip_nat_setup_info(ct, &newrange, hooknum);
187 }
188
189 static struct ipt_target same_reg = { 
190         .name           = "SAME",
191         .target         = same_target,
192         .targetsize     = sizeof(struct ipt_same_info),
193         .table          = "nat",
194         .hooks          = (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
195         .checkentry     = same_check,
196         .destroy        = same_destroy,
197         .me             = THIS_MODULE,
198 };
199
200 static int __init ipt_same_init(void)
201 {
202         return ipt_register_target(&same_reg);
203 }
204
205 static void __exit ipt_same_fini(void)
206 {
207         ipt_unregister_target(&same_reg);
208 }
209
210 module_init(ipt_same_init);
211 module_exit(ipt_same_fini);
212