aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4/netfilter/ipt_SAME.c
blob: a1cdd1262de29acf656a0ba1cb5e74e63584a86f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Same.  Just like SNAT, only try to make the connections
 * 	  between client A and server B always have the same source ip.
 *
 * (C) 2000 Paul `Rusty' Russell
 * (C) 2001 Martin Josefsson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 010320 Martin Josefsson <gandalf@wlug.westbo.se>
 * 	* copied ipt_BALANCE.c to ipt_SAME.c and changed a few things.
 * 010728 Martin Josefsson <gandalf@wlug.westbo.se>
 * 	* added --nodst to not include destination-ip in new source
 * 	  calculations.
 *	* added some more sanity-checks.
 * 010729 Martin Josefsson <gandalf@wlug.westbo.se>
 * 	* fixed a buggy if-statement in same_check(), should have
 * 	  used ntohl() but didn't.
 * 	* added support for multiple ranges. IPT_SAME_MAX_RANGE is
 * 	  defined in linux/include/linux/netfilter_ipv4/ipt_SAME.h
 * 	  and is currently set to 10.
 * 	* added support for 1-address range, nice to have now that
 * 	  we have multiple ranges.
 */
#include <linux/types.h>
#include <linux/ip.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netdevice.h>
#include <linux/if.h>
#include <linux/inetdevice.h>
#include <net/protocol.h>
#include <net/checksum.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter/x_tables.h>
#ifdef CONFIG_NF_NAT_NEEDED
#include <net/netfilter/nf_nat_rule.h>
#else
#include <linux/netfilter_ipv4/ip_nat_rule.h>
#endif
#include <linux/netfilter_ipv4/ipt_SAME.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");

#if 0
#define DEBUGP printk
#else
#define DEBUGP(format, args...)
#endif

static int
same_check(const char *tablename,
	      const void *e,
	      const struct xt_target *target,
	      void *targinfo,
	      unsigned int hook_mask)
{
	unsigned int count, countess, rangeip, index = 0;
	struct ipt_same_info *mr = targinfo;

	mr->ipnum = 0;

	if (mr->rangesize < 1) {
		DEBUGP("same_check: need at least one dest range.\n");
		return 0;
	}
	if (mr->rangesize > IPT_SAME_MAX_RANGE) {
		DEBUGP("same_check: too many ranges specified, maximum "
				"is %u ranges\n",
				IPT_SAME_MAX_RANGE);
		return 0;
	}
	for (count = 0; count < mr->rangesize; count++) {
		if (ntohl(mr->range[count].min_ip) >
				ntohl(mr->range[count].max_ip)) {
			DEBUGP("same_check: min_ip is larger than max_ip in "
				"range `%u.%u.%u.%u-%u.%u.%u.%u'.\n",
				NIPQUAD(mr->range[count].min_ip),
				NIPQUAD(mr->range[count].max_ip));
			return 0;
		}
		if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
			DEBUGP("same_check: bad MAP_IPS.\n");
			return 0;
		}
		rangeip = (ntohl(mr->range[count].max_ip) - 
					ntohl(mr->range[count].min_ip) + 1);
		mr->ipnum += rangeip;
		
		DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
	}
	DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
	
	mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
	if (!mr->iparray) {
		DEBUGP("same_check: Couldn't allocate %u bytes "
			"for %u ipaddresses!\n", 
			(sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
		return 0;
	}
	DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
			(sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
	
	for (count = 0; count < mr->rangesize; count++) {
		for (countess = ntohl(mr->range[count].min_ip);
				countess <= ntohl(mr->range[count].max_ip);
					countess++) {
			mr->iparray[index] = countess;
			DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
				"in index %u.\n",
				HIPQUAD(countess), index);
			index++;
		}
	}
	return 1;
}

static void 
same_destroy(const struct xt_target *target, void *targinfo)
{
	struct ipt_same_info *mr = targinfo;

	kfree(mr->iparray);
	
	DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
			(sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
}

static unsigned int
same_target(struct sk_buff **pskb,
		const struct net_device *in,
		const struct net_device *out,
		unsigned int hooknum,
		const struct xt_target *target,
		const void *targinfo)
{
	struct ip_conntrack *ct;
	enum ip_conntrack_info ctinfo;
	u_int32_t tmpip, aindex;
	__be32 new_ip;
	const struct ipt_same_info *same = targinfo;
	struct ip_nat_range newrange;
	const struct ip_conntrack_tuple *t;

	IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
			hooknum == NF_IP_POST_ROUTING);
	ct = ip_conntrack_get(*pskb, &ctinfo);

	t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;

	/* Base new source on real src ip and optionally dst ip,
	   giving some hope for consistency across reboots.
	   Here we calculate the index in same->iparray which
	   holds the ipaddress we should use */
	
#ifdef CONFIG_NF_NAT_NEEDED
	tmpip = ntohl(t->src.u3.ip);

	if (!(same->info & IPT_SAME_NODST))
		tmpip += ntohl(t->dst.u3.ip);
#else
	tmpip = ntohl(t->src.ip);

	if (!(same->info & IPT_SAME_NODST))
		tmpip += ntohl(t->dst.ip);
#endif
	aindex = tmpip % same->ipnum;

	new_ip = htonl(same->iparray[aindex]);

	DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
			"new src=%u.%u.%u.%u\n",
			NIPQUAD(t->src.ip), NIPQUAD(t->dst.ip),
			NIPQUAD(new_ip));

	/* Transfer from original range. */
	newrange = ((struct ip_nat_range)
		{ same->range[0].flags, new_ip, new_ip,
		  /* FIXME: Use ports from correct range! */
		  same->range[0].min, same->range[0].max });

	/* Hand modified range to generic setup. */
	return ip_nat_setup_info(ct, &newrange, hooknum);
}

static struct xt_target same_reg = {
	.name		= "SAME",
	.family		= AF_INET,
	.target		= same_target,
	.targetsize	= sizeof(struct ipt_same_info),
	.table		= "nat",
	.hooks		= (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
	.checkentry	= same_check,
	.destroy	= same_destroy,
	.me		= THIS_MODULE,
};

static int __init ipt_same_init(void)
{
	return xt_register_target(&same_reg);
}

static void __exit ipt_same_fini(void)
{
	xt_unregister_target(&same_reg);
}

module_init(ipt_same_init);
module_exit(ipt_same_fini);