ipt_CLUSTERIP.c 19.0 KB
Newer Older
1
/* Cluster IP hashmark target
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
 * based on ideas of Fabio Olive Leite <olive@unixforge.org>
 *
 * Development of this code funded by SuSE Linux AG, http://www.suse.com/
 *
 * 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.
 *
 */
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/jhash.h>
15
#include <linux/bitops.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/if_arp.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#include <net/checksum.h>

#include <linux/netfilter_arp.h>

29
#include <linux/netfilter/x_tables.h>
L
Linus Torvalds 已提交
30 31
#include <linux/netfilter_ipv4/ip_tables.h>
#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
32
#include <net/netfilter/nf_conntrack_compat.h>
L
Linus Torvalds 已提交
33

34
#define CLUSTERIP_VERSION "0.8"
L
Linus Torvalds 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#define DEBUG_CLUSTERIP

#ifdef DEBUG_CLUSTERIP
#define DEBUGP	printk
#else
#define DEBUGP
#endif

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
MODULE_DESCRIPTION("iptables target for CLUSTERIP");

struct clusterip_config {
	struct list_head list;			/* list of all configs */
	atomic_t refcount;			/* reference count */
51 52
	atomic_t entries;			/* number of entries/rules
						 * referencing us */
L
Linus Torvalds 已提交
53

A
Al Viro 已提交
54
	__be32 clusterip;			/* the IP address */
L
Linus Torvalds 已提交
55 56 57
	u_int8_t clustermac[ETH_ALEN];		/* the MAC address */
	struct net_device *dev;			/* device */
	u_int16_t num_total_nodes;		/* total number of nodes */
58
	unsigned long local_nodes;		/* node number array */
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68

#ifdef CONFIG_PROC_FS
	struct proc_dir_entry *pde;		/* proc dir entry */
#endif
	enum clusterip_hashmode hash_mode;	/* which hashing mode */
	u_int32_t hash_initval;			/* hash initialization */
};

static LIST_HEAD(clusterip_configs);

69
/* clusterip_lock protects the clusterip_configs list */
P
Patrick McHardy 已提交
70
static DEFINE_RWLOCK(clusterip_lock);
L
Linus Torvalds 已提交
71 72

#ifdef CONFIG_PROC_FS
73
static const struct file_operations clusterip_proc_fops;
L
Linus Torvalds 已提交
74 75 76 77
static struct proc_dir_entry *clusterip_procdir;
#endif

static inline void
78 79
clusterip_config_get(struct clusterip_config *c)
{
L
Linus Torvalds 已提交
80 81 82 83
	atomic_inc(&c->refcount);
}

static inline void
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
clusterip_config_put(struct clusterip_config *c)
{
	if (atomic_dec_and_test(&c->refcount))
		kfree(c);
}

/* increase the count of entries(rules) using/referencing this config */
static inline void
clusterip_config_entry_get(struct clusterip_config *c)
{
	atomic_inc(&c->entries);
}

/* decrease the count of entries using/referencing this config.  If last
 * entry(rule) is removed, remove the config from lists, but don't free it
 * yet, since proc-files could still be holding references */
static inline void
clusterip_config_entry_put(struct clusterip_config *c)
{
	if (atomic_dec_and_test(&c->entries)) {
P
Patrick McHardy 已提交
104
		write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
105
		list_del(&c->list);
P
Patrick McHardy 已提交
106
		write_unlock_bh(&clusterip_lock);
107

L
Linus Torvalds 已提交
108 109
		dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
		dev_put(c->dev);
110 111 112 113 114 115 116

		/* In case anyone still accesses the file, the open/close
		 * functions are also incrementing the refcount on their own,
		 * so it's safe to remove the entry even if it's in use. */
#ifdef CONFIG_PROC_FS
		remove_proc_entry(c->pde->name, c->pde->parent);
#endif
L
Linus Torvalds 已提交
117 118 119 120
	}
}

static struct clusterip_config *
A
Al Viro 已提交
121
__clusterip_config_find(__be32 clusterip)
L
Linus Torvalds 已提交
122 123 124 125
{
	struct list_head *pos;

	list_for_each(pos, &clusterip_configs) {
126
		struct clusterip_config *c = list_entry(pos,
L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136
					struct clusterip_config, list);
		if (c->clusterip == clusterip) {
			return c;
		}
	}

	return NULL;
}

static inline struct clusterip_config *
A
Al Viro 已提交
137
clusterip_config_find_get(__be32 clusterip, int entry)
L
Linus Torvalds 已提交
138 139 140
{
	struct clusterip_config *c;

P
Patrick McHardy 已提交
141
	read_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
142 143
	c = __clusterip_config_find(clusterip);
	if (!c) {
P
Patrick McHardy 已提交
144
		read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
145 146 147
		return NULL;
	}
	atomic_inc(&c->refcount);
148 149
	if (entry)
		atomic_inc(&c->entries);
P
Patrick McHardy 已提交
150
	read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
151 152 153 154

	return c;
}

155 156 157 158 159 160 161 162 163 164 165
static void
clusterip_config_init_nodelist(struct clusterip_config *c,
			       const struct ipt_clusterip_tgt_info *i)
{
	int n;

	for (n = 0; n < i->num_local_nodes; n++) {
		set_bit(i->local_nodes[n] - 1, &c->local_nodes);
	}
}

L
Linus Torvalds 已提交
166
static struct clusterip_config *
A
Al Viro 已提交
167
clusterip_config_init(struct ipt_clusterip_tgt_info *i, __be32 ip,
L
Linus Torvalds 已提交
168 169 170 171
			struct net_device *dev)
{
	struct clusterip_config *c;

172
	c = kzalloc(sizeof(*c), GFP_ATOMIC);
L
Linus Torvalds 已提交
173 174 175 176 177 178 179
	if (!c)
		return NULL;

	c->dev = dev;
	c->clusterip = ip;
	memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
	c->num_total_nodes = i->num_total_nodes;
180
	clusterip_config_init_nodelist(c, i);
L
Linus Torvalds 已提交
181 182 183
	c->hash_mode = i->hash_mode;
	c->hash_initval = i->hash_initval;
	atomic_set(&c->refcount, 1);
184
	atomic_set(&c->entries, 1);
L
Linus Torvalds 已提交
185 186

#ifdef CONFIG_PROC_FS
187 188 189 190 191 192 193 194 195 196 197
	{
		char buffer[16];

		/* create proc dir entry */
		sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));
		c->pde = create_proc_entry(buffer, S_IWUSR|S_IRUSR,
					   clusterip_procdir);
		if (!c->pde) {
			kfree(c);
			return NULL;
		}
L
Linus Torvalds 已提交
198 199 200 201 202
	}
	c->pde->proc_fops = &clusterip_proc_fops;
	c->pde->data = c;
#endif

P
Patrick McHardy 已提交
203
	write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
204
	list_add(&c->list, &clusterip_configs);
P
Patrick McHardy 已提交
205
	write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
206 207 208 209

	return c;
}

210
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
211 212 213 214
static int
clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
{

215 216
	if (nodenum == 0 ||
	    nodenum > c->num_total_nodes)
L
Linus Torvalds 已提交
217 218
		return 1;

219 220 221
	/* check if we already have this number in our bitfield */
	if (test_and_set_bit(nodenum - 1, &c->local_nodes))
		return 1;
L
Linus Torvalds 已提交
222 223 224 225 226 227 228

	return 0;
}

static int
clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
{
229 230
	if (nodenum == 0 ||
	    nodenum > c->num_total_nodes)
L
Linus Torvalds 已提交
231
		return 1;
232

233 234
	if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
		return 0;
L
Linus Torvalds 已提交
235 236 237

	return 1;
}
238
#endif
L
Linus Torvalds 已提交
239 240 241 242 243 244 245

static inline u_int32_t
clusterip_hashfn(struct sk_buff *skb, struct clusterip_config *config)
{
	struct iphdr *iph = skb->nh.iph;
	unsigned long hashval;
	u_int16_t sport, dport;
246
	u_int16_t *ports;
L
Linus Torvalds 已提交
247 248 249 250

	switch (iph->protocol) {
	case IPPROTO_TCP:
	case IPPROTO_UDP:
251
	case IPPROTO_UDPLITE:
252 253
	case IPPROTO_SCTP:
	case IPPROTO_DCCP:
L
Linus Torvalds 已提交
254
	case IPPROTO_ICMP:
255 256 257
		ports = (void *)iph+iph->ihl*4;
		sport = ports[0];
		dport = ports[1];
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		break;
	default:
		if (net_ratelimit()) {
			printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",
				iph->protocol);
		}
		sport = dport = 0;
	}

	switch (config->hash_mode) {
	case CLUSTERIP_HASHMODE_SIP:
		hashval = jhash_1word(ntohl(iph->saddr),
				      config->hash_initval);
		break;
	case CLUSTERIP_HASHMODE_SIP_SPT:
273
		hashval = jhash_2words(ntohl(iph->saddr), sport,
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
				       config->hash_initval);
		break;
	case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
		hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
				       config->hash_initval);
		break;
	default:
		/* to make gcc happy */
		hashval = 0;
		/* This cannot happen, unless the check function wasn't called
		 * at rule load time */
		printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);
		BUG();
		break;
	}

	/* node numbers are 1..n, not 0..n */
	return ((hashval % config->num_total_nodes)+1);
}

static inline int
clusterip_responsible(struct clusterip_config *config, u_int32_t hash)
{
297
	return test_bit(hash - 1, &config->local_nodes);
L
Linus Torvalds 已提交
298 299
}

300 301
/***********************************************************************
 * IPTABLES TARGET
L
Linus Torvalds 已提交
302 303 304 305 306 307 308
 ***********************************************************************/

static unsigned int
target(struct sk_buff **pskb,
       const struct net_device *in,
       const struct net_device *out,
       unsigned int hooknum,
309
       const struct xt_target *target,
310
       const void *targinfo)
L
Linus Torvalds 已提交
311 312 313
{
	const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
	enum ip_conntrack_info ctinfo;
314
	u_int32_t *mark, hash;
L
Linus Torvalds 已提交
315 316 317 318 319

	/* don't need to clusterip_config_get() here, since refcount
	 * is only decremented by destroy() - and ip_tables guarantees
	 * that the ->target() function isn't called after ->destroy() */

320 321
	mark = nf_ct_get_mark((*pskb), &ctinfo);
	if (mark == NULL) {
L
Linus Torvalds 已提交
322 323
		printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
			/* FIXME: need to drop invalid ones, since replies
324
			 * to outgoing connections of other nodes will be
L
Linus Torvalds 已提交
325 326 327 328 329 330 331
			 * marked as INVALID */
		return NF_DROP;
	}

	/* special case: ICMP error handling. conntrack distinguishes between
	 * error messages (RELATED) and information requests (see below) */
	if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP
332
	    && (ctinfo == IP_CT_RELATED
333
		|| ctinfo == IP_CT_RELATED+IP_CT_IS_REPLY))
334
		return XT_CONTINUE;
L
Linus Torvalds 已提交
335

336
	/* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
L
Linus Torvalds 已提交
337 338 339 340 341 342 343
	 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
	 * on, which all have an ID field [relevant for hashing]. */

	hash = clusterip_hashfn(*pskb, cipinfo->config);

	switch (ctinfo) {
		case IP_CT_NEW:
344
			*mark = hash;
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
			break;
		case IP_CT_RELATED:
		case IP_CT_RELATED+IP_CT_IS_REPLY:
			/* FIXME: we don't handle expectations at the
			 * moment.  they can arrive on a different node than
			 * the master connection (e.g. FTP passive mode) */
		case IP_CT_ESTABLISHED:
		case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:
			break;
		default:
			break;
	}

#ifdef DEBUG_CLUSTERP
	DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
#endif
361
	DEBUGP("hash=%u ct_hash=%u ", hash, *mark);
L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371
	if (!clusterip_responsible(cipinfo->config, hash)) {
		DEBUGP("not responsible\n");
		return NF_DROP;
	}
	DEBUGP("responsible\n");

	/* despite being received via linklayer multicast, this is
	 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
	(*pskb)->pkt_type = PACKET_HOST;

372
	return XT_CONTINUE;
L
Linus Torvalds 已提交
373 374 375 376
}

static int
checkentry(const char *tablename,
377
	   const void *e_void,
378
	   const struct xt_target *target,
379 380
	   void *targinfo,
	   unsigned int hook_mask)
L
Linus Torvalds 已提交
381 382
{
	struct ipt_clusterip_tgt_info *cipinfo = targinfo;
383
	const struct ipt_entry *e = e_void;
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394

	struct clusterip_config *config;

	if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
	    cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
	    cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
		printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",
			cipinfo->hash_mode);
		return 0;

	}
A
Al Viro 已提交
395
	if (e->ip.dmsk.s_addr != htonl(0xffffffff)
L
Linus Torvalds 已提交
396 397 398 399 400 401 402
	    || e->ip.dst.s_addr == 0) {
		printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
		return 0;
	}

	/* FIXME: further sanity checks */

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	config = clusterip_config_find_get(e->ip.dst.s_addr, 1);
	if (config) {
		if (cipinfo->config != NULL) {
			/* Case A: This is an entry that gets reloaded, since
			 * it still has a cipinfo->config pointer. Simply
			 * increase the entry refcount and return */
			if (cipinfo->config != config) {
				printk(KERN_ERR "CLUSTERIP: Reloaded entry "
				       "has invalid config pointer!\n");
				return 0;
			}
		} else {
			/* Case B: This is a new rule referring to an existing
			 * clusterip config. */
			cipinfo->config = config;
		}
	} else {
		/* Case C: This is a completely new clusterip config */
L
Linus Torvalds 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
		if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
			printk(KERN_WARNING "CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e->ip.dst.s_addr));
			return 0;
		} else {
			struct net_device *dev;

			if (e->ip.iniface[0] == '\0') {
				printk(KERN_WARNING "CLUSTERIP: Please specify an interface name\n");
				return 0;
			}

			dev = dev_get_by_name(e->ip.iniface);
			if (!dev) {
				printk(KERN_WARNING "CLUSTERIP: no such interface %s\n", e->ip.iniface);
				return 0;
			}

438
			config = clusterip_config_init(cipinfo,
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446
							e->ip.dst.s_addr, dev);
			if (!config) {
				printk(KERN_WARNING "CLUSTERIP: cannot allocate config\n");
				dev_put(dev);
				return 0;
			}
			dev_mc_add(config->dev,config->clustermac, ETH_ALEN, 0);
		}
447
		cipinfo->config = config;
L
Linus Torvalds 已提交
448 449
	}

450 451 452 453 454 455
	if (nf_ct_l3proto_try_module_get(target->family) < 0) {
		printk(KERN_WARNING "can't load conntrack support for "
				    "proto=%d\n", target->family);
		return 0;
	}

L
Linus Torvalds 已提交
456 457 458 459
	return 1;
}

/* drop reference count of cluster config when rule is deleted */
460
static void destroy(const struct xt_target *target, void *targinfo)
L
Linus Torvalds 已提交
461
{
462
	struct ipt_clusterip_tgt_info *cipinfo = targinfo;
L
Linus Torvalds 已提交
463

464 465 466 467
	/* if no more entries are referencing the config, remove it
	 * from the list and destroy the proc entry */
	clusterip_config_entry_put(cipinfo->config);

L
Linus Torvalds 已提交
468
	clusterip_config_put(cipinfo->config);
469 470

	nf_ct_l3proto_module_put(target->family);
L
Linus Torvalds 已提交
471 472
}

473
static struct xt_target clusterip_tgt = {
474
	.name		= "CLUSTERIP",
475
	.family		= AF_INET,
476 477 478 479 480
	.target		= target,
	.targetsize	= sizeof(struct ipt_clusterip_tgt_info),
	.checkentry	= checkentry,
	.destroy	= destroy,
	.me		= THIS_MODULE
L
Linus Torvalds 已提交
481 482 483
};


484 485
/***********************************************************************
 * ARP MANGLING CODE
L
Linus Torvalds 已提交
486 487 488 489 490
 ***********************************************************************/

/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
struct arp_payload {
	u_int8_t src_hw[ETH_ALEN];
A
Al Viro 已提交
491
	__be32 src_ip;
L
Linus Torvalds 已提交
492
	u_int8_t dst_hw[ETH_ALEN];
A
Al Viro 已提交
493
	__be32 dst_ip;
L
Linus Torvalds 已提交
494 495 496
} __attribute__ ((packed));

#ifdef CLUSTERIP_DEBUG
497
static void arp_print(struct arp_payload *payload)
L
Linus Torvalds 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510
{
#define HBUFFERLEN 30
	char hbuffer[HBUFFERLEN];
	int j,k;
	const char hexbuf[]= "0123456789abcdef";

	for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
		hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
		hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
		hbuffer[k++]=':';
	}
	hbuffer[--k]='\0';

511
	printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		NIPQUAD(payload->src_ip), hbuffer,
		NIPQUAD(payload->dst_ip));
}
#endif

static unsigned int
arp_mangle(unsigned int hook,
	   struct sk_buff **pskb,
	   const struct net_device *in,
	   const struct net_device *out,
	   int (*okfn)(struct sk_buff *))
{
	struct arphdr *arp = (*pskb)->nh.arph;
	struct arp_payload *payload;
	struct clusterip_config *c;

	/* we don't care about non-ethernet and non-ipv4 ARP */
	if (arp->ar_hrd != htons(ARPHRD_ETHER)
	    || arp->ar_pro != htons(ETH_P_IP)
	    || arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
		return NF_ACCEPT;

534 535 536
	/* we only want to mangle arp requests and replies */
	if (arp->ar_op != htons(ARPOP_REPLY)
	    && arp->ar_op != htons(ARPOP_REQUEST))
L
Linus Torvalds 已提交
537 538 539 540
		return NF_ACCEPT;

	payload = (void *)(arp+1);

541
	/* if there is no clusterip configuration for the arp reply's
L
Linus Torvalds 已提交
542
	 * source ip, we don't want to mangle it */
543
	c = clusterip_config_find_get(payload->src_ip, 0);
L
Linus Torvalds 已提交
544 545 546
	if (!c)
		return NF_ACCEPT;

547
	/* normally the linux kernel always replies to arp queries of
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	 * addresses on different interfacs.  However, in the CLUSTERIP case
	 * this wouldn't work, since we didn't subscribe the mcast group on
	 * other interfaces */
	if (c->dev != out) {
		DEBUGP("CLUSTERIP: not mangling arp reply on different "
		       "interface: cip'%s'-skb'%s'\n", c->dev->name, out->name);
		clusterip_config_put(c);
		return NF_ACCEPT;
	}

	/* mangle reply hardware address */
	memcpy(payload->src_hw, c->clustermac, arp->ar_hln);

#ifdef CLUSTERIP_DEBUG
	DEBUGP(KERN_DEBUG "CLUSTERIP mangled arp reply: ");
	arp_print(payload);
#endif

	clusterip_config_put(c);

	return NF_ACCEPT;
}

static struct nf_hook_ops cip_arp_ops = {
	.hook = arp_mangle,
	.pf = NF_ARP,
	.hooknum = NF_ARP_OUT,
	.priority = -1
};

578 579
/***********************************************************************
 * PROC DIR HANDLING
L
Linus Torvalds 已提交
580 581 582 583
 ***********************************************************************/

#ifdef CONFIG_PROC_FS

584 585 586 587 588 589 590
struct clusterip_seq_position {
	unsigned int pos;	/* position */
	unsigned int weight;	/* number of bits set == size */
	unsigned int bit;	/* current bit */
	unsigned long val;	/* current value */
};

L
Linus Torvalds 已提交
591 592 593 594
static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
{
	struct proc_dir_entry *pde = s->private;
	struct clusterip_config *c = pde->data;
595 596 597 598 599 600 601 602
	unsigned int weight;
	u_int32_t local_nodes;
	struct clusterip_seq_position *idx;

	/* FIXME: possible race */
	local_nodes = c->local_nodes;
	weight = hweight32(local_nodes);
	if (*pos >= weight)
L
Linus Torvalds 已提交
603 604
		return NULL;

605 606
	idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
	if (!idx)
L
Linus Torvalds 已提交
607 608
		return ERR_PTR(-ENOMEM);

609 610 611 612 613 614 615
	idx->pos = *pos;
	idx->weight = weight;
	idx->bit = ffs(local_nodes);
	idx->val = local_nodes;
	clear_bit(idx->bit - 1, &idx->val);

	return idx;
L
Linus Torvalds 已提交
616 617 618 619
}

static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
620
	struct clusterip_seq_position *idx = (struct clusterip_seq_position *)v;
L
Linus Torvalds 已提交
621

622 623
	*pos = ++idx->pos;
	if (*pos >= idx->weight) {
L
Linus Torvalds 已提交
624 625 626
		kfree(v);
		return NULL;
	}
627 628 629
	idx->bit = ffs(idx->val);
	clear_bit(idx->bit - 1, &idx->val);
	return idx;
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638
}

static void clusterip_seq_stop(struct seq_file *s, void *v)
{
	kfree(v);
}

static int clusterip_seq_show(struct seq_file *s, void *v)
{
639
	struct clusterip_seq_position *idx = (struct clusterip_seq_position *)v;
L
Linus Torvalds 已提交
640

641
	if (idx->pos != 0)
L
Linus Torvalds 已提交
642 643
		seq_putc(s, ',');

644 645 646
	seq_printf(s, "%u", idx->bit);

	if (idx->pos == idx->weight - 1)
L
Linus Torvalds 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		seq_putc(s, '\n');

	return 0;
}

static struct seq_operations clusterip_seq_ops = {
	.start	= clusterip_seq_start,
	.next	= clusterip_seq_next,
	.stop	= clusterip_seq_stop,
	.show	= clusterip_seq_show,
};

static int clusterip_proc_open(struct inode *inode, struct file *file)
{
	int ret = seq_open(file, &clusterip_seq_ops);

	if (!ret) {
		struct seq_file *sf = file->private_data;
		struct proc_dir_entry *pde = PDE(inode);
		struct clusterip_config *c = pde->data;

		sf->private = pde;

		clusterip_config_get(c);
	}

	return ret;
}

static int clusterip_proc_release(struct inode *inode, struct file *file)
{
	struct proc_dir_entry *pde = PDE(inode);
	struct clusterip_config *c = pde->data;
	int ret;

	ret = seq_release(inode, file);

	if (!ret)
		clusterip_config_put(c);

	return ret;
}

static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
				size_t size, loff_t *ofs)
{
#define PROC_WRITELEN	10
	char buffer[PROC_WRITELEN+1];
695
	struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	struct clusterip_config *c = pde->data;
	unsigned long nodenum;

	if (copy_from_user(buffer, input, PROC_WRITELEN))
		return -EFAULT;

	if (*buffer == '+') {
		nodenum = simple_strtoul(buffer+1, NULL, 10);
		if (clusterip_add_node(c, nodenum))
			return -ENOMEM;
	} else if (*buffer == '-') {
		nodenum = simple_strtoul(buffer+1, NULL,10);
		if (clusterip_del_node(c, nodenum))
			return -ENOENT;
	} else
		return -EIO;

	return size;
}

716
static const struct file_operations clusterip_proc_fops = {
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726
	.owner	 = THIS_MODULE,
	.open	 = clusterip_proc_open,
	.read	 = seq_read,
	.write	 = clusterip_proc_write,
	.llseek	 = seq_lseek,
	.release = clusterip_proc_release,
};

#endif /* CONFIG_PROC_FS */

727
static int __init ipt_clusterip_init(void)
L
Linus Torvalds 已提交
728 729 730
{
	int ret;

731
	ret = xt_register_target(&clusterip_tgt);
732 733
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
734

735 736
	ret = nf_register_hook(&cip_arp_ops);
	if (ret < 0)
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
		goto cleanup_target;

#ifdef CONFIG_PROC_FS
	clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", proc_net);
	if (!clusterip_procdir) {
		printk(KERN_ERR "CLUSTERIP: Unable to proc dir entry\n");
		ret = -ENOMEM;
		goto cleanup_hook;
	}
#endif /* CONFIG_PROC_FS */

	printk(KERN_NOTICE "ClusterIP Version %s loaded successfully\n",
		CLUSTERIP_VERSION);
	return 0;

752
#ifdef CONFIG_PROC_FS
L
Linus Torvalds 已提交
753 754
cleanup_hook:
	nf_unregister_hook(&cip_arp_ops);
755
#endif /* CONFIG_PROC_FS */
L
Linus Torvalds 已提交
756
cleanup_target:
757
	xt_unregister_target(&clusterip_tgt);
758
	return ret;
L
Linus Torvalds 已提交
759 760
}

761
static void __exit ipt_clusterip_fini(void)
L
Linus Torvalds 已提交
762
{
763 764 765 766 767 768
	printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
		CLUSTERIP_VERSION);
#ifdef CONFIG_PROC_FS
	remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
#endif
	nf_unregister_hook(&cip_arp_ops);
769
	xt_unregister_target(&clusterip_tgt);
L
Linus Torvalds 已提交
770 771
}

772 773
module_init(ipt_clusterip_init);
module_exit(ipt_clusterip_fini);