blob: fe76ffc0caedbd082fb623c7b54c317f7e50b45b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080037#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080038#include <net/netfilter/nf_nat_rule.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/netfilter_ipv4/ipt_SAME.h>
40
41MODULE_LICENSE("GPL");
42MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
43MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");
44
45#if 0
46#define DEBUGP printk
47#else
48#define DEBUGP(format, args...)
49#endif
50
51static int
52same_check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080053 const void *e,
Patrick McHardyc4986732006-03-20 18:02:56 -080054 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned int hook_mask)
57{
58 unsigned int count, countess, rangeip, index = 0;
59 struct ipt_same_info *mr = targinfo;
60
61 mr->ipnum = 0;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (mr->rangesize < 1) {
64 DEBUGP("same_check: need at least one dest range.\n");
65 return 0;
66 }
67 if (mr->rangesize > IPT_SAME_MAX_RANGE) {
68 DEBUGP("same_check: too many ranges specified, maximum "
69 "is %u ranges\n",
70 IPT_SAME_MAX_RANGE);
71 return 0;
72 }
73 for (count = 0; count < mr->rangesize; count++) {
74 if (ntohl(mr->range[count].min_ip) >
75 ntohl(mr->range[count].max_ip)) {
76 DEBUGP("same_check: min_ip is larger than max_ip in "
77 "range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
78 NIPQUAD(mr->range[count].min_ip),
79 NIPQUAD(mr->range[count].max_ip));
80 return 0;
81 }
82 if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
83 DEBUGP("same_check: bad MAP_IPS.\n");
84 return 0;
85 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090086 rangeip = (ntohl(mr->range[count].max_ip) -
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ntohl(mr->range[count].min_ip) + 1);
88 mr->ipnum += rangeip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
91 }
92 DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
95 if (!mr->iparray) {
96 DEBUGP("same_check: Couldn't allocate %u bytes "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090097 "for %u ipaddresses!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
99 return 0;
100 }
101 DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
102 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 for (count = 0; count < mr->rangesize; count++) {
105 for (countess = ntohl(mr->range[count].min_ip);
106 countess <= ntohl(mr->range[count].max_ip);
107 countess++) {
108 mr->iparray[index] = countess;
109 DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
110 "in index %u.\n",
111 HIPQUAD(countess), index);
112 index++;
113 }
114 }
115 return 1;
116}
117
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900118static void
Patrick McHardyefa74162006-08-22 00:36:37 -0700119same_destroy(const struct xt_target *target, void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct ipt_same_info *mr = targinfo;
122
123 kfree(mr->iparray);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
126 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
127}
128
129static unsigned int
130same_target(struct sk_buff **pskb,
131 const struct net_device *in,
132 const struct net_device *out,
133 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800134 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700135 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Patrick McHardy587aa6412007-03-14 16:37:25 -0700137 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 enum ip_conntrack_info ctinfo;
Al Viro6a19d612006-09-28 14:22:24 -0700139 u_int32_t tmpip, aindex;
140 __be32 new_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 const struct ipt_same_info *same = targinfo;
Patrick McHardy587aa6412007-03-14 16:37:25 -0700142 struct nf_nat_range newrange;
143 const struct nf_conntrack_tuple *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Patrick McHardy587aa6412007-03-14 16:37:25 -0700145 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 hooknum == NF_IP_POST_ROUTING);
Patrick McHardy587aa6412007-03-14 16:37:25 -0700147 ct = nf_ct_get(*pskb, &ctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
150
151 /* Base new source on real src ip and optionally dst ip,
152 giving some hope for consistency across reboots.
153 Here we calculate the index in same->iparray which
154 holds the ipaddress we should use */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800156 tmpip = ntohl(t->src.u3.ip);
157
158 if (!(same->info & IPT_SAME_NODST))
159 tmpip += ntohl(t->dst.u3.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 aindex = tmpip % same->ipnum;
161
162 new_ip = htonl(same->iparray[aindex]);
163
164 DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
165 "new src=%u.%u.%u.%u\n",
166 NIPQUAD(t->src.ip), NIPQUAD(t->dst.ip),
167 NIPQUAD(new_ip));
168
169 /* Transfer from original range. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700170 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 { same->range[0].flags, new_ip, new_ip,
172 /* FIXME: Use ports from correct range! */
173 same->range[0].min, same->range[0].max });
174
175 /* Hand modified range to generic setup. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700176 return nf_nat_setup_info(ct, &newrange, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800179static struct xt_target same_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .name = "SAME",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800181 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .target = same_target,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800183 .targetsize = sizeof(struct ipt_same_info),
184 .table = "nat",
185 .hooks = (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .checkentry = same_check,
187 .destroy = same_destroy,
188 .me = THIS_MODULE,
189};
190
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800191static int __init ipt_same_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800193 return xt_register_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800196static void __exit ipt_same_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800198 xt_unregister_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800201module_init(ipt_same_init);
202module_exit(ipt_same_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203