[NETFILTER]: x_tables: mark matches and targets __read_mostly
[deliverable/linux.git] / net / ipv4 / netfilter / ipt_SAME.c
1 /* 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 #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>
22 #include <linux/netfilter/x_tables.h>
23 #include <net/netfilter/nf_nat_rule.h>
24 #include <linux/netfilter_ipv4/ipt_SAME.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Martin Josefsson <gandalf@wlug.westbo.se>");
28 MODULE_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
36 static bool
37 same_check(const char *tablename,
38 const void *e,
39 const struct xt_target *target,
40 void *targinfo,
41 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
48 if (mr->rangesize < 1) {
49 DEBUGP("same_check: need at least one dest range.\n");
50 return false;
51 }
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);
56 return false;
57 }
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));
65 return false;
66 }
67 if (!(mr->range[count].flags & IP_NAT_RANGE_MAP_IPS)) {
68 DEBUGP("same_check: bad MAP_IPS.\n");
69 return false;
70 }
71 rangeip = (ntohl(mr->range[count].max_ip) -
72 ntohl(mr->range[count].min_ip) + 1);
73 mr->ipnum += rangeip;
74
75 DEBUGP("same_check: range %u, ipnum = %u\n", count, rangeip);
76 }
77 DEBUGP("same_check: total ipaddresses = %u\n", mr->ipnum);
78
79 mr->iparray = kmalloc((sizeof(u_int32_t) * mr->ipnum), GFP_KERNEL);
80 if (!mr->iparray) {
81 DEBUGP("same_check: Couldn't allocate %u bytes "
82 "for %u ipaddresses!\n",
83 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
84 return false;
85 }
86 DEBUGP("same_check: Allocated %u bytes for %u ipaddresses.\n",
87 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
88
89 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 }
100 return true;
101 }
102
103 static void
104 same_destroy(const struct xt_target *target, void *targinfo)
105 {
106 struct ipt_same_info *mr = targinfo;
107
108 kfree(mr->iparray);
109
110 DEBUGP("same_destroy: Deallocated %u bytes for %u ipaddresses.\n",
111 (sizeof(u_int32_t) * mr->ipnum), mr->ipnum);
112 }
113
114 static unsigned int
115 same_target(struct sk_buff **pskb,
116 const struct net_device *in,
117 const struct net_device *out,
118 unsigned int hooknum,
119 const struct xt_target *target,
120 const void *targinfo)
121 {
122 struct nf_conn *ct;
123 enum ip_conntrack_info ctinfo;
124 u_int32_t tmpip, aindex;
125 __be32 new_ip;
126 const struct ipt_same_info *same = targinfo;
127 struct nf_nat_range newrange;
128 const struct nf_conntrack_tuple *t;
129
130 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING ||
131 hooknum == NF_IP_POST_ROUTING);
132 ct = nf_ct_get(*pskb, &ctinfo);
133
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 */
140
141 tmpip = ntohl(t->src.u3.ip);
142
143 if (!(same->info & IPT_SAME_NODST))
144 tmpip += ntohl(t->dst.u3.ip);
145 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. */
155 newrange = ((struct nf_nat_range)
156 { 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. */
161 return nf_nat_setup_info(ct, &newrange, hooknum);
162 }
163
164 static struct xt_target same_reg __read_mostly = {
165 .name = "SAME",
166 .family = AF_INET,
167 .target = same_target,
168 .targetsize = sizeof(struct ipt_same_info),
169 .table = "nat",
170 .hooks = (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_POST_ROUTING),
171 .checkentry = same_check,
172 .destroy = same_destroy,
173 .me = THIS_MODULE,
174 };
175
176 static int __init ipt_same_init(void)
177 {
178 return xt_register_target(&same_reg);
179 }
180
181 static void __exit ipt_same_fini(void)
182 {
183 xt_unregister_target(&same_reg);
184 }
185
186 module_init(ipt_same_init);
187 module_exit(ipt_same_fini);
188
This page took 0.037751 seconds and 5 git commands to generate.