blob: 3649fabc04ea9f8beb147569b6fbe8157caaf0ea [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#include <linux/types.h>
12#include <linux/ip.h>
13#include <linux/timer.h>
14#include <linux/module.h>
15#include <linux/netfilter.h>
16#include <linux/netdevice.h>
17#include <linux/if.h>
18#include <linux/inetdevice.h>
19#include <net/protocol.h>
20#include <net/checksum.h>
21#include <linux/netfilter_ipv4.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080022#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080023#include <net/netfilter/nf_nat_rule.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/netfilter_ipv4/ipt_SAME.h>
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
28MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");
29
30#if 0
31#define DEBUGP printk
32#else
33#define DEBUGP(format, args...)
34#endif
35
Jan Engelhardte1931b72007-07-07 22:16:26 -070036static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070037same_check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 const void *e,
Patrick McHardyc4986732006-03-20 18:02:56 -080039 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 unsigned int hook_mask)
42{
43 unsigned int count, countess, rangeip, index = 0;
44 struct ipt_same_info *mr = targinfo;
45
46 mr->ipnum = 0;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 if (mr->rangesize < 1) {
49 DEBUGP("same_check: need at least one dest range.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070050 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
52 if (mr->rangesize > IPT_SAME_MAX_RANGE) {
53 DEBUGP("same_check: too many ranges specified, maximum "
54 "is %u ranges\n",
55 IPT_SAME_MAX_RANGE);
Jan Engelhardte1931b72007-07-07 22:16:26 -070056 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58 for (count = 0; count < mr->rangesize; count++) {
59 if (ntohl(mr->range[count].min_ip) >
60 ntohl(mr->range[count].max_ip)) {
61 DEBUGP("same_check: min_ip is larger than max_ip in "
62 "range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
63 NIPQUAD(mr->range[count].min_ip),
64 NIPQUAD(mr->range[count].max_ip));
Jan Engelhardte1931b72007-07-07 22:16:26 -070065 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
67 if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
68 DEBUGP("same_check: bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070069 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090071 rangeip = (ntohl(mr->range[count].max_ip) -
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 ntohl(mr->range[count].min_ip) + 1);
73 mr->ipnum += rangeip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
76 }
77 DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
80 if (!mr->iparray) {
81 DEBUGP("same_check: Couldn't allocate %u bytes "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090082 "for %u ipaddresses!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
Jan Engelhardte1931b72007-07-07 22:16:26 -070084 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
87 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 for (count = 0; count < mr->rangesize; count++) {
90 for (countess = ntohl(mr->range[count].min_ip);
91 countess <= ntohl(mr->range[count].max_ip);
92 countess++) {
93 mr->iparray[index] = countess;
94 DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
95 "in index %u.\n",
96 HIPQUAD(countess), index);
97 index++;
98 }
99 }
Jan Engelhardte1931b72007-07-07 22:16:26 -0700100 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900103static void
Patrick McHardyefa74162006-08-22 00:36:37 -0700104same_destroy(const struct xt_target *target, void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct ipt_same_info *mr = targinfo;
107
108 kfree(mr->iparray);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
111 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
112}
113
114static unsigned int
115same_target(struct sk_buff **pskb,
116 const struct net_device *in,
117 const struct net_device *out,
118 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800119 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700120 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Patrick McHardy587aa6412007-03-14 16:37:25 -0700122 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 enum ip_conntrack_info ctinfo;
Al Viro6a19d612006-09-28 14:22:24 -0700124 u_int32_t tmpip, aindex;
125 __be32 new_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 const struct ipt_same_info *same = targinfo;
Patrick McHardy587aa6412007-03-14 16:37:25 -0700127 struct nf_nat_range newrange;
128 const struct nf_conntrack_tuple *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Patrick McHardy587aa6412007-03-14 16:37:25 -0700130 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 hooknum == NF_IP_POST_ROUTING);
Patrick McHardy587aa6412007-03-14 16:37:25 -0700132 ct = nf_ct_get(*pskb, &ctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
135
136 /* Base new source on real src ip and optionally dst ip,
137 giving some hope for consistency across reboots.
138 Here we calculate the index in same->iparray which
139 holds the ipaddress we should use */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900140
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800141 tmpip = ntohl(t->src.u3.ip);
142
143 if (!(same->info & IPT_SAME_NODST))
144 tmpip += ntohl(t->dst.u3.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 aindex = tmpip % same->ipnum;
146
147 new_ip = htonl(same->iparray[aindex]);
148
149 DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
150 "new src=%u.%u.%u.%u\n",
151 NIPQUAD(t->src.ip), NIPQUAD(t->dst.ip),
152 NIPQUAD(new_ip));
153
154 /* Transfer from original range. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700155 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 { same->range[0].flags, new_ip, new_ip,
157 /* FIXME: Use ports from correct range! */
158 same->range[0].min, same->range[0].max });
159
160 /* Hand modified range to generic setup. */
Patrick McHardy587aa6412007-03-14 16:37:25 -0700161 return nf_nat_setup_info(ct, &newrange, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800164static struct xt_target same_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .name = "SAME",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800166 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .target = same_target,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800168 .targetsize = sizeof(struct ipt_same_info),
169 .table = "nat",
170 .hooks = (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .checkentry = same_check,
172 .destroy = same_destroy,
173 .me = THIS_MODULE,
174};
175
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800176static int __init ipt_same_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800178 return xt_register_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800181static void __exit ipt_same_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800183 xt_unregister_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800186module_init(ipt_same_init);
187module_exit(ipt_same_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188