1 /* Same. Just like SNAT, only try to make the connections
2 * between client A and server B always have the same source ip.
4 * (C) 2000 Paul `Rusty' Russell
5 * (C) 2001 Martin Josefsson
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.
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
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.
26 #include <linux/types.h>
28 #include <linux/timer.h>
29 #include <linux/module.h>
30 #include <linux/netfilter.h>
31 #include <linux/netdevice.h>
33 #include <linux/inetdevice.h>
34 #include <net/protocol.h>
35 #include <net/checksum.h>
36 #include <linux/netfilter_ipv4.h>
37 #include <linux/netfilter_ipv4/ip_nat_rule.h>
38 #include <linux/netfilter_ipv4/ipt_SAME.h>
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
42 MODULE_DESCRIPTION("iptables special SNAT module for consistent sourceip");
47 #define DEBUGP(format, args...)
51 same_check(const char *tablename
,
53 const struct xt_target
*target
,
55 unsigned int targinfosize
,
56 unsigned int hook_mask
)
58 unsigned int count
, countess
, rangeip
, index
= 0;
59 struct ipt_same_info
*mr
= targinfo
;
63 if (mr
->rangesize
< 1) {
64 DEBUGP("same_check: need at least one dest range.\n");
67 if (mr
->rangesize
> IPT_SAME_MAX_RANGE
) {
68 DEBUGP("same_check: too many ranges specified, maximum "
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
));
82 if (!(mr
->range
[count
].flags
& IP_NAT_RANGE_MAP_IPS
)) {
83 DEBUGP("same_check: bad MAP_IPS.\n");
86 rangeip
= (ntohl(mr
->range
[count
].max_ip
) -
87 ntohl(mr
->range
[count
].min_ip
) + 1);
90 DEBUGP("same_check: range %u, ipnum = %u\n", count
, rangeip
);
92 DEBUGP("same_check: total ipaddresses = %u\n", mr
->ipnum
);
94 mr
->iparray
= kmalloc((sizeof(u_int32_t
) * mr
->ipnum
), GFP_KERNEL
);
96 DEBUGP("same_check: Couldn't allocate %u bytes "
97 "for %u ipaddresses!\n",
98 (sizeof(u_int32_t
) * mr
->ipnum
), mr
->ipnum
);
101 DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
102 (sizeof(u_int32_t
) * mr
->ipnum
), mr
->ipnum
);
104 for (count
= 0; count
< mr
->rangesize
; count
++) {
105 for (countess
= ntohl(mr
->range
[count
].min_ip
);
106 countess
<= ntohl(mr
->range
[count
].max_ip
);
108 mr
->iparray
[index
] = countess
;
109 DEBUGP("same_check: Added ipaddress `%u.%u.%u.%u' "
111 HIPQUAD(countess
), index
);
119 same_destroy(const struct xt_target
*target
, void *targinfo
,
120 unsigned int targinfosize
)
122 struct ipt_same_info
*mr
= targinfo
;
126 DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
127 (sizeof(u_int32_t
) * mr
->ipnum
), mr
->ipnum
);
131 same_target(struct sk_buff
**pskb
,
132 const struct net_device
*in
,
133 const struct net_device
*out
,
134 unsigned int hooknum
,
135 const struct xt_target
*target
,
136 const void *targinfo
,
139 struct ip_conntrack
*ct
;
140 enum ip_conntrack_info ctinfo
;
141 u_int32_t tmpip
, aindex
, new_ip
;
142 const struct ipt_same_info
*same
= targinfo
;
143 struct ip_nat_range newrange
;
144 const struct ip_conntrack_tuple
*t
;
146 IP_NF_ASSERT(hooknum
== NF_IP_PRE_ROUTING
||
147 hooknum
== NF_IP_POST_ROUTING
);
148 ct
= ip_conntrack_get(*pskb
, &ctinfo
);
150 t
= &ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
;
152 /* Base new source on real src ip and optionally dst ip,
153 giving some hope for consistency across reboots.
154 Here we calculate the index in same->iparray which
155 holds the ipaddress we should use */
157 tmpip
= ntohl(t
->src
.ip
);
159 if (!(same
->info
& IPT_SAME_NODST
))
160 tmpip
+= ntohl(t
->dst
.ip
);
162 aindex
= tmpip
% same
->ipnum
;
164 new_ip
= htonl(same
->iparray
[aindex
]);
166 DEBUGP("ipt_SAME: src=%u.%u.%u.%u dst=%u.%u.%u.%u, "
167 "new src=%u.%u.%u.%u\n",
168 NIPQUAD(t
->src
.ip
), NIPQUAD(t
->dst
.ip
),
171 /* Transfer from original range. */
172 newrange
= ((struct ip_nat_range
)
173 { same
->range
[0].flags
, new_ip
, new_ip
,
174 /* FIXME: Use ports from correct range! */
175 same
->range
[0].min
, same
->range
[0].max
});
177 /* Hand modified range to generic setup. */
178 return ip_nat_setup_info(ct
, &newrange
, hooknum
);
181 static struct ipt_target same_reg
= {
183 .target
= same_target
,
184 .targetsize
= sizeof(struct ipt_same_info
),
186 .hooks
= (1 << NF_IP_PRE_ROUTING
| 1 << NF_IP_POST_ROUTING
),
187 .checkentry
= same_check
,
188 .destroy
= same_destroy
,
192 static int __init
ipt_same_init(void)
194 return ipt_register_target(&same_reg
);
197 static void __exit
ipt_same_fini(void)
199 ipt_unregister_target(&same_reg
);
202 module_init(ipt_same_init
);
203 module_exit(ipt_same_fini
);