blob: 97641f1a97f6078f4482401bf5d3a7be30443c40 [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
Jan Engelhardte1931b72007-07-07 22:16:26 -070030static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070031same_check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -080032 const void *e,
Patrick McHardyc4986732006-03-20 18:02:56 -080033 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 void *targinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned int hook_mask)
36{
37 unsigned int count, countess, rangeip, index = 0;
38 struct ipt_same_info *mr = targinfo;
39
40 mr->ipnum = 0;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (mr->rangesize < 1) {
Patrick McHardy0d537782007-07-07 22:39:38 -070043 pr_debug("same_check: need at least one dest range.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070044 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 }
46 if (mr->rangesize > IPT_SAME_MAX_RANGE) {
Patrick McHardy0d537782007-07-07 22:39:38 -070047 pr_debug("same_check: too many ranges specified, maximum "
48 "is %u ranges\n", IPT_SAME_MAX_RANGE);
Jan Engelhardte1931b72007-07-07 22:16:26 -070049 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51 for (count = 0; count < mr->rangesize; count++) {
52 if (ntohl(mr->range[count].min_ip) >
53 ntohl(mr->range[count].max_ip)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070054 pr_debug("same_check: min_ip is larger than max_ip in "
55 "range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
56 NIPQUAD(mr->range[count].min_ip),
57 NIPQUAD(mr->range[count].max_ip));
Jan Engelhardte1931b72007-07-07 22:16:26 -070058 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
Patrick McHardy0d537782007-07-07 22:39:38 -070061 pr_debug("same_check: bad MAP_IPS.\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -070062 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090064 rangeip = (ntohl(mr->range[count].max_ip) -
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 ntohl(mr->range[count].min_ip) + 1);
66 mr->ipnum += rangeip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090067
Patrick McHardy0d537782007-07-07 22:39:38 -070068 pr_debug("same_check: range %u, ipnum = %u\n", count, rangeip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Patrick McHardy0d537782007-07-07 22:39:38 -070070 pr_debug("same_check: total ipaddresses = %u\n", mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
73 if (!mr->iparray) {
Patrick McHardy0d537782007-07-07 22:39:38 -070074 pr_debug("same_check: Couldn't allocate %Zu bytes "
75 "for %u ipaddresses!\n",
76 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
Jan Engelhardte1931b72007-07-07 22:16:26 -070077 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Patrick McHardy0d537782007-07-07 22:39:38 -070079 pr_debug("same_check: Allocated %Zu bytes for %u ipaddresses.\n",
80 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 for (count = 0; count < mr->rangesize; count++) {
83 for (countess = ntohl(mr->range[count].min_ip);
84 countess <= ntohl(mr->range[count].max_ip);
85 countess++) {
86 mr->iparray[index] = countess;
Patrick McHardy0d537782007-07-07 22:39:38 -070087 pr_debug("same_check: Added ipaddress `%u.%u.%u.%u' "
88 "in index %u.\n", HIPQUAD(countess), index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 index++;
90 }
91 }
Jan Engelhardte1931b72007-07-07 22:16:26 -070092 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090095static void
Patrick McHardyefa74162006-08-22 00:36:37 -070096same_destroy(const struct xt_target *target, void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct ipt_same_info *mr = targinfo;
99
100 kfree(mr->iparray);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900101
Patrick McHardy0d537782007-07-07 22:39:38 -0700102 pr_debug("same_destroy: Deallocated %Zu bytes for %u ipaddresses.\n",
103 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static unsigned int
107same_target(struct sk_buff **pskb,
108 const struct net_device *in,
109 const struct net_device *out,
110 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800111 const struct xt_target *target,
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700112 const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Patrick McHardy587aa642007-03-14 16:37:25 -0700114 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 enum ip_conntrack_info ctinfo;
Al Viro6a19d612006-09-28 14:22:24 -0700116 u_int32_t tmpip, aindex;
117 __be32 new_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 const struct ipt_same_info *same = targinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -0700119 struct nf_nat_range newrange;
120 const struct nf_conntrack_tuple *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick McHardy587aa642007-03-14 16:37:25 -0700122 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 hooknum == NF_IP_POST_ROUTING);
Patrick McHardy587aa642007-03-14 16:37:25 -0700124 ct = nf_ct_get(*pskb, &ctinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
127
128 /* Base new source on real src ip and optionally dst ip,
129 giving some hope for consistency across reboots.
130 Here we calculate the index in same->iparray which
131 holds the ipaddress we should use */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900132
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800133 tmpip = ntohl(t->src.u3.ip);
134
135 if (!(same->info & IPT_SAME_NODST))
136 tmpip += ntohl(t->dst.u3.ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 aindex = tmpip % same->ipnum;
138
139 new_ip = htonl(same->iparray[aindex]);
140
Patrick McHardy0d537782007-07-07 22:39:38 -0700141 pr_debug("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
142 "new src=%u.%u.%u.%u\n",
143 NIPQUAD(t->src.u3.ip), NIPQUAD(t->dst.u3.ip), NIPQUAD(new_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* Transfer from original range. */
Patrick McHardy587aa642007-03-14 16:37:25 -0700146 newrange = ((struct nf_nat_range)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 { same->range[0].flags, new_ip, new_ip,
148 /* FIXME: Use ports from correct range! */
149 same->range[0].min, same->range[0].max });
150
151 /* Hand modified range to generic setup. */
Patrick McHardy587aa642007-03-14 16:37:25 -0700152 return nf_nat_setup_info(ct, &newrange, hooknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Patrick McHardy9f15c532007-07-07 22:22:02 -0700155static struct xt_target same_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .name = "SAME",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800157 .family = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 .target = same_target,
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800159 .targetsize = sizeof(struct ipt_same_info),
160 .table = "nat",
161 .hooks = (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 .checkentry = same_check,
163 .destroy = same_destroy,
164 .me = THIS_MODULE,
165};
166
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800167static int __init ipt_same_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800169 return xt_register_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800172static void __exit ipt_same_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800174 xt_unregister_target(&same_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800177module_init(ipt_same_init);
178module_exit(ipt_same_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179