ipt_CLUSTERIP.c 18.4 KB
Newer Older
L
Linus Torvalds 已提交
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
/* Cluster IP hashmark target 
 * (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/config.h>
#include <linux/proc_fs.h>
#include <linux/jhash.h>
#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>

#include <linux/netfilter_ipv4/ip_tables.h>
#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
#include <linux/netfilter_ipv4/ip_conntrack.h>

#define CLUSTERIP_VERSION "0.6"

#define DEBUG_CLUSTERIP

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

P
Patrick McHardy 已提交
43 44
#define ASSERT_READ_LOCK(x)

L
Linus Torvalds 已提交
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
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 */

	u_int32_t clusterip;			/* the IP address */
	u_int8_t clustermac[ETH_ALEN];		/* the MAC address */
	struct net_device *dev;			/* device */
	u_int16_t num_total_nodes;		/* total number of nodes */
	u_int16_t num_local_nodes;		/* number of local nodes */
	u_int16_t local_nodes[CLUSTERIP_MAX_NODES];	/* node number array */

#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);

/* clusterip_lock protects the clusterip_configs list _AND_ the configurable
 * data within all structurses (num_local_nodes, local_nodes[]) */
P
Patrick McHardy 已提交
71
static DEFINE_RWLOCK(clusterip_lock);
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85

#ifdef CONFIG_PROC_FS
static struct file_operations clusterip_proc_fops;
static struct proc_dir_entry *clusterip_procdir;
#endif

static inline void
clusterip_config_get(struct clusterip_config *c) {
	atomic_inc(&c->refcount);
}

static inline void
clusterip_config_put(struct clusterip_config *c) {
	if (atomic_dec_and_test(&c->refcount)) {
P
Patrick McHardy 已提交
86
		write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
87
		list_del(&c->list);
P
Patrick McHardy 已提交
88
		write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100
		dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
		dev_put(c->dev);
		kfree(c);
	}
}


static struct clusterip_config *
__clusterip_config_find(u_int32_t clusterip)
{
	struct list_head *pos;

P
Patrick McHardy 已提交
101
	ASSERT_READ_LOCK(&clusterip_lock);
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	list_for_each(pos, &clusterip_configs) {
		struct clusterip_config *c = list_entry(pos, 
					struct clusterip_config, list);
		if (c->clusterip == clusterip) {
			return c;
		}
	}

	return NULL;
}

static inline struct clusterip_config *
clusterip_config_find_get(u_int32_t clusterip)
{
	struct clusterip_config *c;

P
Patrick McHardy 已提交
118
	read_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
119 120
	c = __clusterip_config_find(clusterip);
	if (!c) {
P
Patrick McHardy 已提交
121
		read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
122 123 124
		return NULL;
	}
	atomic_inc(&c->refcount);
P
Patrick McHardy 已提交
125
	read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
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

	return c;
}

static struct clusterip_config *
clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
			struct net_device *dev)
{
	struct clusterip_config *c;
	char buffer[16];

	c = kmalloc(sizeof(*c), GFP_ATOMIC);
	if (!c)
		return NULL;

	memset(c, 0, sizeof(*c));
	c->dev = dev;
	c->clusterip = ip;
	memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
	c->num_total_nodes = i->num_total_nodes;
	c->num_local_nodes = i->num_local_nodes;
	memcpy(&c->local_nodes, &i->local_nodes, sizeof(&c->local_nodes));
	c->hash_mode = i->hash_mode;
	c->hash_initval = i->hash_initval;
	atomic_set(&c->refcount, 1);

#ifdef CONFIG_PROC_FS
	/* 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;
	}
	c->pde->proc_fops = &clusterip_proc_fops;
	c->pde->data = c;
#endif

P
Patrick McHardy 已提交
164
	write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
165
	list_add(&c->list, &clusterip_configs);
P
Patrick McHardy 已提交
166
	write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175

	return c;
}

static int
clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
{
	int i;

P
Patrick McHardy 已提交
176
	write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
177 178 179

	if (c->num_local_nodes >= CLUSTERIP_MAX_NODES
	    || nodenum > CLUSTERIP_MAX_NODES) {
P
Patrick McHardy 已提交
180
		write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
181 182 183 184 185 186
		return 1;
	}

	/* check if we alrady have this number in our array */
	for (i = 0; i < c->num_local_nodes; i++) {
		if (c->local_nodes[i] == nodenum) {
P
Patrick McHardy 已提交
187
			write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
188 189 190 191 192 193
			return 1;
		}
	}

	c->local_nodes[c->num_local_nodes++] = nodenum;

P
Patrick McHardy 已提交
194
	write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202
	return 0;
}

static int
clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
{
	int i;

P
Patrick McHardy 已提交
203
	write_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
204 205

	if (c->num_local_nodes <= 1 || nodenum > CLUSTERIP_MAX_NODES) {
P
Patrick McHardy 已提交
206
		write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214
		return 1;
	}
		
	for (i = 0; i < c->num_local_nodes; i++) {
		if (c->local_nodes[i] == nodenum) {
			int size = sizeof(u_int16_t)*(c->num_local_nodes-(i+1));
			memmove(&c->local_nodes[i], &c->local_nodes[i+1], size);
			c->num_local_nodes--;
P
Patrick McHardy 已提交
215
			write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
216 217 218 219
			return 0;
		}
	}

P
Patrick McHardy 已提交
220
	write_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	return 1;
}

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;
	struct tcphdr *th;
	struct udphdr *uh;
	struct icmphdr *ih;

	switch (iph->protocol) {
	case IPPROTO_TCP:
		th = (void *)iph+iph->ihl*4;
		sport = ntohs(th->source);
		dport = ntohs(th->dest);
		break;
	case IPPROTO_UDP:
		uh = (void *)iph+iph->ihl*4;
		sport = ntohs(uh->source);
		dport = ntohs(uh->dest);
		break;
	case IPPROTO_ICMP:
		ih = (void *)iph+iph->ihl*4;
		sport = ntohs(ih->un.echo.id);
		dport = (ih->type<<8)|ih->code;
		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:
		hashval = jhash_2words(ntohl(iph->saddr), sport, 
				       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)
{
	int i;

P
Patrick McHardy 已提交
290
	read_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
291 292

	if (config->num_local_nodes == 0) {
P
Patrick McHardy 已提交
293
		read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
294 295 296 297 298
		return 0;
	}

	for (i = 0; i < config->num_local_nodes; i++) {
		if (config->local_nodes[i] == hash) {
P
Patrick McHardy 已提交
299
			read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
300 301 302 303
			return 1;
		}
	}

P
Patrick McHardy 已提交
304
	read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 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 578 579 580 581

	return 0;
}

/*********************************************************************** 
 * IPTABLES TARGET 
 ***********************************************************************/

static unsigned int
target(struct sk_buff **pskb,
       const struct net_device *in,
       const struct net_device *out,
       unsigned int hooknum,
       const void *targinfo,
       void *userinfo)
{
	const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
	enum ip_conntrack_info ctinfo;
	struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
	u_int32_t hash;

	/* 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() */

	if (!ct) {
		printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
			/* FIXME: need to drop invalid ones, since replies
			 * to outgoing connections of other nodes will be 
			 * 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
	    && (ctinfo == IP_CT_RELATED 
		|| ctinfo == IP_CT_IS_REPLY+IP_CT_IS_REPLY))
		return IPT_CONTINUE;

	/* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO, 
	 * 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:
			ct->mark = hash;
			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
	DEBUGP("hash=%u ct_hash=%lu ", hash, ct->mark);
	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;

	return IPT_CONTINUE;
}

static int
checkentry(const char *tablename,
	   const struct ipt_entry *e,
           void *targinfo,
           unsigned int targinfosize,
           unsigned int hook_mask)
{
	struct ipt_clusterip_tgt_info *cipinfo = targinfo;

	struct clusterip_config *config;

	if (targinfosize != IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info))) {
		printk(KERN_WARNING "CLUSTERIP: targinfosize %u != %Zu\n",
		       targinfosize,
		       IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)));
		return 0;
	}

	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;

	}
	if (e->ip.dmsk.s_addr != 0xffffffff
	    || e->ip.dst.s_addr == 0) {
		printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
		return 0;
	}

	/* FIXME: further sanity checks */

	config = clusterip_config_find_get(e->ip.dst.s_addr);
	if (!config) {
		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;
			}

			config = clusterip_config_init(cipinfo, 
							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);
		}
	}

	cipinfo->config = config;

	return 1;
}

/* drop reference count of cluster config when rule is deleted */
static void destroy(void *matchinfo, unsigned int matchinfosize)
{
	struct ipt_clusterip_tgt_info *cipinfo = matchinfo;

	/* we first remove the proc entry and then drop the reference
	 * count.  In case anyone still accesses the file, the open/close
	 * functions are also incrementing the refcount on their own */
#ifdef CONFIG_PROC_FS
	remove_proc_entry(cipinfo->config->pde->name,
			  cipinfo->config->pde->parent);
#endif
	clusterip_config_put(cipinfo->config);
}

static struct ipt_target clusterip_tgt = { 
	.name = "CLUSTERIP",
	.target = &target, 
	.checkentry = &checkentry, 
	.destroy = &destroy,
	.me = THIS_MODULE
};


/*********************************************************************** 
 * ARP MANGLING CODE 
 ***********************************************************************/

/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
struct arp_payload {
	u_int8_t src_hw[ETH_ALEN];
	u_int32_t src_ip;
	u_int8_t dst_hw[ETH_ALEN];
	u_int32_t dst_ip;
} __attribute__ ((packed));

#ifdef CLUSTERIP_DEBUG
static void arp_print(struct arp_payload *payload) 
{
#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';

	printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n", 
		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;

	/* we only want to mangle arp replies */
	if (arp->ar_op != htons(ARPOP_REPLY))
		return NF_ACCEPT;

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

	/* if there is no clusterip configuration for the arp reply's 
	 * source ip, we don't want to mangle it */
	c = clusterip_config_find_get(payload->src_ip);
	if (!c)
		return NF_ACCEPT;

	/* normally the linux kernel always replies to arp queries of 
	 * 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
};

/*********************************************************************** 
 * PROC DIR HANDLING 
 ***********************************************************************/

#ifdef CONFIG_PROC_FS

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;
	unsigned int *nodeidx;

P
Patrick McHardy 已提交
582
	read_lock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	if (*pos >= c->num_local_nodes)
		return NULL;

	nodeidx = kmalloc(sizeof(unsigned int), GFP_KERNEL);
	if (!nodeidx)
		return ERR_PTR(-ENOMEM);

	*nodeidx = *pos;
	return nodeidx;
}

static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	struct proc_dir_entry *pde = s->private;
	struct clusterip_config *c = pde->data;
	unsigned int *nodeidx = (unsigned int *)v;

	*pos = ++(*nodeidx);
	if (*pos >= c->num_local_nodes) {
		kfree(v);
		return NULL;
	}
	return nodeidx;
}

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

P
Patrick McHardy 已提交
612
	read_unlock_bh(&clusterip_lock);
L
Linus Torvalds 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 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 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
}

static int clusterip_seq_show(struct seq_file *s, void *v)
{
	struct proc_dir_entry *pde = s->private;
	struct clusterip_config *c = pde->data;
	unsigned int *nodeidx = (unsigned int *)v;

	if (*nodeidx != 0) 
		seq_putc(s, ',');
	seq_printf(s, "%u", c->local_nodes[*nodeidx]);

	if (*nodeidx == c->num_local_nodes-1)
		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];
	struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
	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;
}

static struct file_operations clusterip_proc_fops = {
	.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 */

static int init_or_cleanup(int fini)
{
	int ret;

	if (fini)
		goto cleanup;

	if (ipt_register_target(&clusterip_tgt)) {
		ret = -EINVAL;
		goto cleanup_none;
	}

	if (nf_register_hook(&cip_arp_ops) < 0) {
		ret = -EINVAL;
		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;

cleanup:
	printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
		CLUSTERIP_VERSION);
#ifdef CONFIG_PROC_FS
	remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
#endif
cleanup_hook:
	nf_unregister_hook(&cip_arp_ops);
cleanup_target:
	ipt_unregister_target(&clusterip_tgt);
cleanup_none:
	return -EINVAL;
}

static int __init init(void)
{
	return init_or_cleanup(0);
}

static void __exit fini(void)
{
	init_or_cleanup(1);
}

module_init(init);
module_exit(fini);