blob: 2510d4fcdb5477aed31047e4983347c859055f27 [file] [log] [blame]
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001/* Cluster IP hashmark target
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
3 * based on ideas of Fabio Olive Leite <olive@unixforge.org>
4 *
5 * Development of this code funded by SuSE Linux AG, http://www.suse.com/
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 */
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/proc_fs.h>
14#include <linux/jhash.h>
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070015#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/skbuff.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/udp.h>
20#include <linux/icmp.h>
21#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/netfilter_arp.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080024#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netfilter_ipv4/ip_tables.h>
26#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070027#include <net/netfilter/nf_conntrack.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070029#include <net/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070031#define CLUSTERIP_VERSION "0.8"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080035MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct clusterip_config {
38 struct list_head list; /* list of all configs */
39 atomic_t refcount; /* reference count */
KOVACS Krisztian44513622005-09-16 16:59:46 -070040 atomic_t entries; /* number of entries/rules
41 * referencing us */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Al Viro6a19d612006-09-28 14:22:24 -070043 __be32 clusterip; /* the IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
45 struct net_device *dev; /* device */
46 u_int16_t num_total_nodes; /* total number of nodes */
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070047 unsigned long local_nodes; /* node number array */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#ifdef CONFIG_PROC_FS
50 struct proc_dir_entry *pde; /* proc dir entry */
51#endif
52 enum clusterip_hashmode hash_mode; /* which hashing mode */
53 u_int32_t hash_initval; /* hash initialization */
54};
55
56static LIST_HEAD(clusterip_configs);
57
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070058/* clusterip_lock protects the clusterip_configs list */
Patrick McHardye45b1be2005-06-21 14:01:30 -070059static DEFINE_RWLOCK(clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -080062static const struct file_operations clusterip_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct proc_dir_entry *clusterip_procdir;
64#endif
65
66static inline void
KOVACS Krisztian44513622005-09-16 16:59:46 -070067clusterip_config_get(struct clusterip_config *c)
68{
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 atomic_inc(&c->refcount);
70}
71
72static inline void
KOVACS Krisztian44513622005-09-16 16:59:46 -070073clusterip_config_put(struct clusterip_config *c)
74{
75 if (atomic_dec_and_test(&c->refcount))
76 kfree(c);
77}
78
KOVACS Krisztian44513622005-09-16 16:59:46 -070079/* decrease the count of entries using/referencing this config. If last
80 * entry(rule) is removed, remove the config from lists, but don't free it
81 * yet, since proc-files could still be holding references */
82static inline void
83clusterip_config_entry_put(struct clusterip_config *c)
84{
85 if (atomic_dec_and_test(&c->entries)) {
Patrick McHardye45b1be2005-06-21 14:01:30 -070086 write_lock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 list_del(&c->list);
Patrick McHardye45b1be2005-06-21 14:01:30 -070088 write_unlock_bh(&clusterip_lock);
KOVACS Krisztian44513622005-09-16 16:59:46 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
91 dev_put(c->dev);
KOVACS Krisztian44513622005-09-16 16:59:46 -070092
93 /* In case anyone still accesses the file, the open/close
94 * functions are also incrementing the refcount on their own,
95 * so it's safe to remove the entry even if it's in use. */
96#ifdef CONFIG_PROC_FS
97 remove_proc_entry(c->pde->name, c->pde->parent);
98#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static struct clusterip_config *
Al Viro6a19d612006-09-28 14:22:24 -0700103__clusterip_config_find(__be32 clusterip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Li Zefan4c610972007-12-04 23:22:26 -0800105 struct clusterip_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Li Zefan4c610972007-12-04 23:22:26 -0800107 list_for_each_entry(c, &clusterip_configs, list) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700108 if (c->clusterip == clusterip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 return NULL;
113}
114
115static inline struct clusterip_config *
Al Viro6a19d612006-09-28 14:22:24 -0700116clusterip_config_find_get(__be32 clusterip, int entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct clusterip_config *c;
119
Patrick McHardye45b1be2005-06-21 14:01:30 -0700120 read_lock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 c = __clusterip_config_find(clusterip);
122 if (!c) {
Patrick McHardye45b1be2005-06-21 14:01:30 -0700123 read_unlock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return NULL;
125 }
126 atomic_inc(&c->refcount);
KOVACS Krisztian44513622005-09-16 16:59:46 -0700127 if (entry)
128 atomic_inc(&c->entries);
Patrick McHardye45b1be2005-06-21 14:01:30 -0700129 read_unlock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return c;
132}
133
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700134static void
135clusterip_config_init_nodelist(struct clusterip_config *c,
136 const struct ipt_clusterip_tgt_info *i)
137{
138 int n;
139
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700140 for (n = 0; n < i->num_local_nodes; n++)
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700141 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct clusterip_config *
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200145clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct net_device *dev)
147{
148 struct clusterip_config *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700150 c = kzalloc(sizeof(*c), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!c)
152 return NULL;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 c->dev = dev;
155 c->clusterip = ip;
156 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
157 c->num_total_nodes = i->num_total_nodes;
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700158 clusterip_config_init_nodelist(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 c->hash_mode = i->hash_mode;
160 c->hash_initval = i->hash_initval;
161 atomic_set(&c->refcount, 1);
KOVACS Krisztian44513622005-09-16 16:59:46 -0700162 atomic_set(&c->entries, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164#ifdef CONFIG_PROC_FS
Patrick McHardy76592582006-11-29 02:35:42 +0100165 {
166 char buffer[16];
167
168 /* create proc dir entry */
169 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700170 c->pde = proc_create(buffer, S_IWUSR|S_IRUSR,
171 clusterip_procdir, &clusterip_proc_fops);
Patrick McHardy76592582006-11-29 02:35:42 +0100172 if (!c->pde) {
173 kfree(c);
174 return NULL;
175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 c->pde->data = c;
178#endif
179
Patrick McHardye45b1be2005-06-21 14:01:30 -0700180 write_lock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 list_add(&c->list, &clusterip_configs);
Patrick McHardye45b1be2005-06-21 14:01:30 -0700182 write_unlock_bh(&clusterip_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 return c;
185}
186
Patrick McHardy76592582006-11-29 02:35:42 +0100187#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int
189clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700192 if (nodenum == 0 ||
193 nodenum > c->num_total_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700196 /* check if we already have this number in our bitfield */
197 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
198 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201}
202
Jan Engelhardte1931b72007-07-07 22:16:26 -0700203static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
205{
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700206 if (nodenum == 0 ||
207 nodenum > c->num_total_nodes)
Jan Engelhardte1931b72007-07-07 22:16:26 -0700208 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900209
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700210 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700211 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jan Engelhardte1931b72007-07-07 22:16:26 -0700213 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
Patrick McHardy76592582006-11-29 02:35:42 +0100215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217static inline u_int32_t
Jan Engelhardta47362a2007-07-07 22:16:55 -0700218clusterip_hashfn(const struct sk_buff *skb,
219 const struct clusterip_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700221 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 unsigned long hashval;
223 u_int16_t sport, dport;
Jan Engelhardta47362a2007-07-07 22:16:55 -0700224 const u_int16_t *ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 switch (iph->protocol) {
227 case IPPROTO_TCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 case IPPROTO_UDP:
Patrick McHardya8d0f952007-02-07 15:07:43 -0800229 case IPPROTO_UDPLITE:
Patrick McHardy957dc802006-05-29 18:19:56 -0700230 case IPPROTO_SCTP:
231 case IPPROTO_DCCP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 case IPPROTO_ICMP:
Jan Engelhardta47362a2007-07-07 22:16:55 -0700233 ports = (const void *)iph+iph->ihl*4;
Patrick McHardy957dc802006-05-29 18:19:56 -0700234 sport = ports[0];
235 dport = ports[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237 default:
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700238 if (net_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",
240 iph->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 sport = dport = 0;
242 }
243
244 switch (config->hash_mode) {
245 case CLUSTERIP_HASHMODE_SIP:
246 hashval = jhash_1word(ntohl(iph->saddr),
247 config->hash_initval);
248 break;
249 case CLUSTERIP_HASHMODE_SIP_SPT:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900250 hashval = jhash_2words(ntohl(iph->saddr), sport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 config->hash_initval);
252 break;
253 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
254 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
255 config->hash_initval);
256 break;
257 default:
258 /* to make gcc happy */
259 hashval = 0;
260 /* This cannot happen, unless the check function wasn't called
261 * at rule load time */
262 printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);
263 BUG();
264 break;
265 }
266
267 /* node numbers are 1..n, not 0..n */
Patrick McHardy34498822007-12-17 22:45:52 -0800268 return (((u64)hashval * config->num_total_nodes) >> 32) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271static inline int
Jan Engelhardta47362a2007-07-07 22:16:55 -0700272clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700274 return test_bit(hash - 1, &config->local_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900277/***********************************************************************
278 * IPTABLES TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ***********************************************************************/
280
281static unsigned int
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800282clusterip_tg(struct sk_buff *skb, const struct net_device *in,
283 const struct net_device *out, unsigned int hooknum,
284 const struct xt_target *target, const void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -0700287 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 enum ip_conntrack_info ctinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -0700289 u_int32_t hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* don't need to clusterip_config_get() here, since refcount
292 * is only decremented by destroy() - and ip_tables guarantees
293 * that the ->target() function isn't called after ->destroy() */
294
Herbert Xu3db05fe2007-10-15 00:53:15 -0700295 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy587aa642007-03-14 16:37:25 -0700296 if (ct == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
298 /* FIXME: need to drop invalid ones, since replies
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900299 * to outgoing connections of other nodes will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * marked as INVALID */
301 return NF_DROP;
302 }
303
304 /* special case: ICMP error handling. conntrack distinguishes between
305 * error messages (RELATED) and information requests (see below) */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700306 if (ip_hdr(skb)->protocol == IPPROTO_ICMP
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900307 && (ctinfo == IP_CT_RELATED
Harald Welte5d927eb2005-06-22 12:37:50 -0700308 || ctinfo == IP_CT_RELATED+IP_CT_IS_REPLY))
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800309 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900311 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
313 * on, which all have an ID field [relevant for hashing]. */
314
Herbert Xu3db05fe2007-10-15 00:53:15 -0700315 hash = clusterip_hashfn(skb, cipinfo->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 switch (ctinfo) {
318 case IP_CT_NEW:
Patrick McHardy587aa642007-03-14 16:37:25 -0700319 ct->mark = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 case IP_CT_RELATED:
322 case IP_CT_RELATED+IP_CT_IS_REPLY:
323 /* FIXME: we don't handle expectations at the
324 * moment. they can arrive on a different node than
325 * the master connection (e.g. FTP passive mode) */
326 case IP_CT_ESTABLISHED:
327 case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:
328 break;
329 default:
330 break;
331 }
332
Patrick McHardy0d537782007-07-07 22:39:38 -0700333#ifdef DEBUG
Patrick McHardy30c69fe2008-03-25 20:06:59 -0700334 NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#endif
Patrick McHardy0d537782007-07-07 22:39:38 -0700336 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!clusterip_responsible(cipinfo->config, hash)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700338 pr_debug("not responsible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return NF_DROP;
340 }
Patrick McHardy0d537782007-07-07 22:39:38 -0700341 pr_debug("responsible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* despite being received via linklayer multicast, this is
344 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700345 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800347 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Jan Engelhardte1931b72007-07-07 22:16:26 -0700350static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800351clusterip_tg_check(const char *tablename, const void *e_void,
352 const struct xt_target *target, void *targinfo,
353 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct ipt_clusterip_tgt_info *cipinfo = targinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800356 const struct ipt_entry *e = e_void;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 struct clusterip_config *config;
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
361 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
362 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
363 printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",
364 cipinfo->hash_mode);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700365 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 }
Al Viro6a19d612006-09-28 14:22:24 -0700368 if (e->ip.dmsk.s_addr != htonl(0xffffffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 || e->ip.dst.s_addr == 0) {
370 printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700371 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
374 /* FIXME: further sanity checks */
375
KOVACS Krisztian44513622005-09-16 16:59:46 -0700376 config = clusterip_config_find_get(e->ip.dst.s_addr, 1);
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700377 if (!config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
379 printk(KERN_WARNING "CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e->ip.dst.s_addr));
Jan Engelhardte1931b72007-07-07 22:16:26 -0700380 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
382 struct net_device *dev;
383
384 if (e->ip.iniface[0] == '\0') {
385 printk(KERN_WARNING "CLUSTERIP: Please specify an interface name\n");
Jan Engelhardte1931b72007-07-07 22:16:26 -0700386 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Eric W. Biederman881d9662007-09-17 11:56:21 -0700389 dev = dev_get_by_name(&init_net, e->ip.iniface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!dev) {
391 printk(KERN_WARNING "CLUSTERIP: no such interface %s\n", e->ip.iniface);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700392 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900395 config = clusterip_config_init(cipinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 e->ip.dst.s_addr, dev);
397 if (!config) {
398 printk(KERN_WARNING "CLUSTERIP: cannot allocate config\n");
399 dev_put(dev);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700400 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 dev_mc_add(config->dev,config->clustermac, ETH_ALEN, 0);
403 }
404 }
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700405 cipinfo->config = config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800407 if (nf_ct_l3proto_try_module_get(target->family) < 0) {
408 printk(KERN_WARNING "can't load conntrack support for "
Jan Engelhardtdf54aae2007-12-17 22:43:15 -0800409 "proto=%u\n", target->family);
Jan Engelhardte1931b72007-07-07 22:16:26 -0700410 return false;
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800411 }
412
Jan Engelhardte1931b72007-07-07 22:16:26 -0700413 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416/* drop reference count of cluster config when rule is deleted */
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800417static void clusterip_tg_destroy(const struct xt_target *target, void *targinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200419 const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
KOVACS Krisztian44513622005-09-16 16:59:46 -0700421 /* if no more entries are referencing the config, remove it
422 * from the list and destroy the proc entry */
423 clusterip_config_entry_put(cipinfo->config);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 clusterip_config_put(cipinfo->config);
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800426
427 nf_ct_l3proto_module_put(target->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700430#ifdef CONFIG_COMPAT
431struct compat_ipt_clusterip_tgt_info
432{
433 u_int32_t flags;
434 u_int8_t clustermac[6];
435 u_int16_t num_total_nodes;
436 u_int16_t num_local_nodes;
437 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
438 u_int32_t hash_mode;
439 u_int32_t hash_initval;
440 compat_uptr_t config;
441};
442#endif /* CONFIG_COMPAT */
443
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800444static struct xt_target clusterip_tg_reg __read_mostly = {
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800445 .name = "CLUSTERIP",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800446 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800447 .target = clusterip_tg,
448 .checkentry = clusterip_tg_check,
449 .destroy = clusterip_tg_destroy,
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700450 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
451#ifdef CONFIG_COMPAT
452 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
453#endif /* CONFIG_COMPAT */
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800454 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
457
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900458/***********************************************************************
459 * ARP MANGLING CODE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ***********************************************************************/
461
462/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
463struct arp_payload {
464 u_int8_t src_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700465 __be32 src_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 u_int8_t dst_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700467 __be32 dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468} __attribute__ ((packed));
469
Patrick McHardy0d537782007-07-07 22:39:38 -0700470#ifdef DEBUG
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900471static void arp_print(struct arp_payload *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473#define HBUFFERLEN 30
474 char hbuffer[HBUFFERLEN];
475 int j,k;
476 const char hexbuf[]= "0123456789abcdef";
477
478 for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
479 hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
480 hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
481 hbuffer[k++]=':';
482 }
483 hbuffer[--k]='\0';
484
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900485 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 NIPQUAD(payload->src_ip), hbuffer,
487 NIPQUAD(payload->dst_ip));
488}
489#endif
490
491static unsigned int
492arp_mangle(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700493 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 const struct net_device *in,
495 const struct net_device *out,
496 int (*okfn)(struct sk_buff *))
497{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700498 struct arphdr *arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct arp_payload *payload;
500 struct clusterip_config *c;
501
502 /* we don't care about non-ethernet and non-ipv4 ARP */
503 if (arp->ar_hrd != htons(ARPHRD_ETHER)
504 || arp->ar_pro != htons(ETH_P_IP)
505 || arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
506 return NF_ACCEPT;
507
Harald Welte4095ebf2005-06-28 12:49:30 -0700508 /* we only want to mangle arp requests and replies */
509 if (arp->ar_op != htons(ARPOP_REPLY)
510 && arp->ar_op != htons(ARPOP_REQUEST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return NF_ACCEPT;
512
513 payload = (void *)(arp+1);
514
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900515 /* if there is no clusterip configuration for the arp reply's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * source ip, we don't want to mangle it */
KOVACS Krisztian44513622005-09-16 16:59:46 -0700517 c = clusterip_config_find_get(payload->src_ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!c)
519 return NF_ACCEPT;
520
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900521 /* normally the linux kernel always replies to arp queries of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * addresses on different interfacs. However, in the CLUSTERIP case
523 * this wouldn't work, since we didn't subscribe the mcast group on
524 * other interfaces */
525 if (c->dev != out) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700526 pr_debug("CLUSTERIP: not mangling arp reply on different "
527 "interface: cip'%s'-skb'%s'\n",
528 c->dev->name, out->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 clusterip_config_put(c);
530 return NF_ACCEPT;
531 }
532
533 /* mangle reply hardware address */
534 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
535
Patrick McHardy0d537782007-07-07 22:39:38 -0700536#ifdef DEBUG
537 pr_debug(KERN_DEBUG "CLUSTERIP mangled arp reply: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 arp_print(payload);
539#endif
540
541 clusterip_config_put(c);
542
543 return NF_ACCEPT;
544}
545
Patrick McHardy19994142007-12-05 01:23:00 -0800546static struct nf_hook_ops cip_arp_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .hook = arp_mangle,
548 .pf = NF_ARP,
549 .hooknum = NF_ARP_OUT,
550 .priority = -1
551};
552
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900553/***********************************************************************
554 * PROC DIR HANDLING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ***********************************************************************/
556
557#ifdef CONFIG_PROC_FS
558
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700559struct clusterip_seq_position {
560 unsigned int pos; /* position */
561 unsigned int weight; /* number of bits set == size */
562 unsigned int bit; /* current bit */
563 unsigned long val; /* current value */
564};
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
567{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200568 const struct proc_dir_entry *pde = s->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct clusterip_config *c = pde->data;
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700570 unsigned int weight;
571 u_int32_t local_nodes;
572 struct clusterip_seq_position *idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700574 /* FIXME: possible race */
575 local_nodes = c->local_nodes;
576 weight = hweight32(local_nodes);
577 if (*pos >= weight)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700580 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
581 if (!idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ERR_PTR(-ENOMEM);
583
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700584 idx->pos = *pos;
585 idx->weight = weight;
586 idx->bit = ffs(local_nodes);
587 idx->val = local_nodes;
588 clear_bit(idx->bit - 1, &idx->val);
589
590 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
594{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200595 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700597 *pos = ++idx->pos;
598 if (*pos >= idx->weight) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 kfree(v);
600 return NULL;
601 }
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700602 idx->bit = ffs(idx->val);
603 clear_bit(idx->bit - 1, &idx->val);
604 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607static void clusterip_seq_stop(struct seq_file *s, void *v)
608{
609 kfree(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static int clusterip_seq_show(struct seq_file *s, void *v)
613{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200614 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900616 if (idx->pos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 seq_putc(s, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700619 seq_printf(s, "%u", idx->bit);
620
621 if (idx->pos == idx->weight - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 seq_putc(s, '\n');
623
624 return 0;
625}
626
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700627static const struct seq_operations clusterip_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 .start = clusterip_seq_start,
629 .next = clusterip_seq_next,
630 .stop = clusterip_seq_stop,
631 .show = clusterip_seq_show,
632};
633
634static int clusterip_proc_open(struct inode *inode, struct file *file)
635{
636 int ret = seq_open(file, &clusterip_seq_ops);
637
638 if (!ret) {
639 struct seq_file *sf = file->private_data;
640 struct proc_dir_entry *pde = PDE(inode);
641 struct clusterip_config *c = pde->data;
642
643 sf->private = pde;
644
645 clusterip_config_get(c);
646 }
647
648 return ret;
649}
650
651static int clusterip_proc_release(struct inode *inode, struct file *file)
652{
653 struct proc_dir_entry *pde = PDE(inode);
654 struct clusterip_config *c = pde->data;
655 int ret;
656
657 ret = seq_release(inode, file);
658
659 if (!ret)
660 clusterip_config_put(c);
661
662 return ret;
663}
664
665static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
666 size_t size, loff_t *ofs)
667{
668#define PROC_WRITELEN 10
669 char buffer[PROC_WRITELEN+1];
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200670 const struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct clusterip_config *c = pde->data;
672 unsigned long nodenum;
673
674 if (copy_from_user(buffer, input, PROC_WRITELEN))
675 return -EFAULT;
676
677 if (*buffer == '+') {
678 nodenum = simple_strtoul(buffer+1, NULL, 10);
679 if (clusterip_add_node(c, nodenum))
680 return -ENOMEM;
681 } else if (*buffer == '-') {
682 nodenum = simple_strtoul(buffer+1, NULL,10);
683 if (clusterip_del_node(c, nodenum))
684 return -ENOENT;
685 } else
686 return -EIO;
687
688 return size;
689}
690
Arjan van de Ven9a321442007-02-12 00:55:35 -0800691static const struct file_operations clusterip_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 .owner = THIS_MODULE,
693 .open = clusterip_proc_open,
694 .read = seq_read,
695 .write = clusterip_proc_write,
696 .llseek = seq_lseek,
697 .release = clusterip_proc_release,
698};
699
700#endif /* CONFIG_PROC_FS */
701
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800702static int __init clusterip_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int ret;
705
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800706 ret = xt_register_target(&clusterip_tg_reg);
Patrick McHardy32292a72006-04-06 14:11:30 -0700707 if (ret < 0)
708 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick McHardy32292a72006-04-06 14:11:30 -0700710 ret = nf_register_hook(&cip_arp_ops);
711 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto cleanup_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714#ifdef CONFIG_PROC_FS
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200715 clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", init_net.proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!clusterip_procdir) {
717 printk(KERN_ERR "CLUSTERIP: Unable to proc dir entry\n");
718 ret = -ENOMEM;
719 goto cleanup_hook;
720 }
721#endif /* CONFIG_PROC_FS */
722
723 printk(KERN_NOTICE "ClusterIP Version %s loaded successfully\n",
724 CLUSTERIP_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726
Patrick McHardy76592582006-11-29 02:35:42 +0100727#ifdef CONFIG_PROC_FS
Patrick McHardy32292a72006-04-06 14:11:30 -0700728cleanup_hook:
729 nf_unregister_hook(&cip_arp_ops);
Patrick McHardy76592582006-11-29 02:35:42 +0100730#endif /* CONFIG_PROC_FS */
Patrick McHardy32292a72006-04-06 14:11:30 -0700731cleanup_target:
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800732 xt_unregister_target(&clusterip_tg_reg);
Patrick McHardy32292a72006-04-06 14:11:30 -0700733 return ret;
734}
735
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800736static void __exit clusterip_tg_exit(void)
Patrick McHardy32292a72006-04-06 14:11:30 -0700737{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
739 CLUSTERIP_VERSION);
740#ifdef CONFIG_PROC_FS
741 remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
742#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 nf_unregister_hook(&cip_arp_ops);
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800744 xt_unregister_target(&clusterip_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800747module_init(clusterip_tg_init);
748module_exit(clusterip_tg_exit);