]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter/ipt_CLUSTERIP.c
[NETFILTER] CLUSTERIP: introduce reference counting for entries
[net-next-2.6.git] / net / ipv4 / netfilter / ipt_CLUSTERIP.c
CommitLineData
1da177e4
LT
1/* Cluster IP hashmark target
2 * (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>
13#include <linux/config.h>
14#include <linux/proc_fs.h>
15#include <linux/jhash.h>
16#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>
22#include <linux/proc_fs.h>
23#include <linux/seq_file.h>
24
25#include <net/checksum.h>
26
27#include <linux/netfilter_arp.h>
28
29#include <linux/netfilter_ipv4/ip_tables.h>
30#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
31#include <linux/netfilter_ipv4/ip_conntrack.h>
1da177e4 32
4095ebf1 33#define CLUSTERIP_VERSION "0.7"
1da177e4
LT
34
35#define DEBUG_CLUSTERIP
36
37#ifdef DEBUG_CLUSTERIP
38#define DEBUGP printk
39#else
40#define DEBUGP
41#endif
42
e45b1be8
PM
43#define ASSERT_READ_LOCK(x)
44
1da177e4
LT
45MODULE_LICENSE("GPL");
46MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
47MODULE_DESCRIPTION("iptables target for CLUSTERIP");
48
49struct clusterip_config {
50 struct list_head list; /* list of all configs */
51 atomic_t refcount; /* reference count */
44513624
KK
52 atomic_t entries; /* number of entries/rules
53 * referencing us */
1da177e4
LT
54
55 u_int32_t clusterip; /* the IP address */
56 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
57 struct net_device *dev; /* device */
58 u_int16_t num_total_nodes; /* total number of nodes */
59 u_int16_t num_local_nodes; /* number of local nodes */
60 u_int16_t local_nodes[CLUSTERIP_MAX_NODES]; /* node number array */
61
62#ifdef CONFIG_PROC_FS
63 struct proc_dir_entry *pde; /* proc dir entry */
64#endif
65 enum clusterip_hashmode hash_mode; /* which hashing mode */
66 u_int32_t hash_initval; /* hash initialization */
67};
68
69static LIST_HEAD(clusterip_configs);
70
71/* clusterip_lock protects the clusterip_configs list _AND_ the configurable
72 * data within all structurses (num_local_nodes, local_nodes[]) */
e45b1be8 73static DEFINE_RWLOCK(clusterip_lock);
1da177e4
LT
74
75#ifdef CONFIG_PROC_FS
76static struct file_operations clusterip_proc_fops;
77static struct proc_dir_entry *clusterip_procdir;
78#endif
79
80static inline void
44513624
KK
81clusterip_config_get(struct clusterip_config *c)
82{
1da177e4
LT
83 atomic_inc(&c->refcount);
84}
85
86static inline void
44513624
KK
87clusterip_config_put(struct clusterip_config *c)
88{
89 if (atomic_dec_and_test(&c->refcount))
90 kfree(c);
91}
92
93/* increase the count of entries(rules) using/referencing this config */
94static inline void
95clusterip_config_entry_get(struct clusterip_config *c)
96{
97 atomic_inc(&c->entries);
98}
99
100/* decrease the count of entries using/referencing this config. If last
101 * entry(rule) is removed, remove the config from lists, but don't free it
102 * yet, since proc-files could still be holding references */
103static inline void
104clusterip_config_entry_put(struct clusterip_config *c)
105{
106 if (atomic_dec_and_test(&c->entries)) {
e45b1be8 107 write_lock_bh(&clusterip_lock);
1da177e4 108 list_del(&c->list);
e45b1be8 109 write_unlock_bh(&clusterip_lock);
44513624 110
1da177e4
LT
111 dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
112 dev_put(c->dev);
44513624
KK
113
114 /* In case anyone still accesses the file, the open/close
115 * functions are also incrementing the refcount on their own,
116 * so it's safe to remove the entry even if it's in use. */
117#ifdef CONFIG_PROC_FS
118 remove_proc_entry(c->pde->name, c->pde->parent);
119#endif
1da177e4
LT
120 }
121}
122
1da177e4
LT
123static struct clusterip_config *
124__clusterip_config_find(u_int32_t clusterip)
125{
126 struct list_head *pos;
127
e45b1be8 128 ASSERT_READ_LOCK(&clusterip_lock);
1da177e4
LT
129 list_for_each(pos, &clusterip_configs) {
130 struct clusterip_config *c = list_entry(pos,
131 struct clusterip_config, list);
132 if (c->clusterip == clusterip) {
133 return c;
134 }
135 }
136
137 return NULL;
138}
139
140static inline struct clusterip_config *
44513624 141clusterip_config_find_get(u_int32_t clusterip, int entry)
1da177e4
LT
142{
143 struct clusterip_config *c;
144
e45b1be8 145 read_lock_bh(&clusterip_lock);
1da177e4
LT
146 c = __clusterip_config_find(clusterip);
147 if (!c) {
e45b1be8 148 read_unlock_bh(&clusterip_lock);
1da177e4
LT
149 return NULL;
150 }
151 atomic_inc(&c->refcount);
44513624
KK
152 if (entry)
153 atomic_inc(&c->entries);
e45b1be8 154 read_unlock_bh(&clusterip_lock);
1da177e4
LT
155
156 return c;
157}
158
159static struct clusterip_config *
160clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
161 struct net_device *dev)
162{
163 struct clusterip_config *c;
164 char buffer[16];
165
166 c = kmalloc(sizeof(*c), GFP_ATOMIC);
167 if (!c)
168 return NULL;
169
170 memset(c, 0, sizeof(*c));
171 c->dev = dev;
172 c->clusterip = ip;
173 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
174 c->num_total_nodes = i->num_total_nodes;
175 c->num_local_nodes = i->num_local_nodes;
5170dbeb 176 memcpy(&c->local_nodes, &i->local_nodes, sizeof(c->local_nodes));
1da177e4
LT
177 c->hash_mode = i->hash_mode;
178 c->hash_initval = i->hash_initval;
179 atomic_set(&c->refcount, 1);
44513624 180 atomic_set(&c->entries, 1);
1da177e4
LT
181
182#ifdef CONFIG_PROC_FS
183 /* create proc dir entry */
184 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));
185 c->pde = create_proc_entry(buffer, S_IWUSR|S_IRUSR, clusterip_procdir);
186 if (!c->pde) {
187 kfree(c);
188 return NULL;
189 }
190 c->pde->proc_fops = &clusterip_proc_fops;
191 c->pde->data = c;
192#endif
193
e45b1be8 194 write_lock_bh(&clusterip_lock);
1da177e4 195 list_add(&c->list, &clusterip_configs);
e45b1be8 196 write_unlock_bh(&clusterip_lock);
1da177e4
LT
197
198 return c;
199}
200
201static int
202clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
203{
204 int i;
205
e45b1be8 206 write_lock_bh(&clusterip_lock);
1da177e4
LT
207
208 if (c->num_local_nodes >= CLUSTERIP_MAX_NODES
209 || nodenum > CLUSTERIP_MAX_NODES) {
e45b1be8 210 write_unlock_bh(&clusterip_lock);
1da177e4
LT
211 return 1;
212 }
213
214 /* check if we alrady have this number in our array */
215 for (i = 0; i < c->num_local_nodes; i++) {
216 if (c->local_nodes[i] == nodenum) {
e45b1be8 217 write_unlock_bh(&clusterip_lock);
1da177e4
LT
218 return 1;
219 }
220 }
221
222 c->local_nodes[c->num_local_nodes++] = nodenum;
223
e45b1be8 224 write_unlock_bh(&clusterip_lock);
1da177e4
LT
225 return 0;
226}
227
228static int
229clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
230{
231 int i;
232
e45b1be8 233 write_lock_bh(&clusterip_lock);
1da177e4
LT
234
235 if (c->num_local_nodes <= 1 || nodenum > CLUSTERIP_MAX_NODES) {
e45b1be8 236 write_unlock_bh(&clusterip_lock);
1da177e4
LT
237 return 1;
238 }
239
240 for (i = 0; i < c->num_local_nodes; i++) {
241 if (c->local_nodes[i] == nodenum) {
242 int size = sizeof(u_int16_t)*(c->num_local_nodes-(i+1));
243 memmove(&c->local_nodes[i], &c->local_nodes[i+1], size);
244 c->num_local_nodes--;
e45b1be8 245 write_unlock_bh(&clusterip_lock);
1da177e4
LT
246 return 0;
247 }
248 }
249
e45b1be8 250 write_unlock_bh(&clusterip_lock);
1da177e4
LT
251 return 1;
252}
253
254static inline u_int32_t
255clusterip_hashfn(struct sk_buff *skb, struct clusterip_config *config)
256{
257 struct iphdr *iph = skb->nh.iph;
258 unsigned long hashval;
259 u_int16_t sport, dport;
260 struct tcphdr *th;
261 struct udphdr *uh;
262 struct icmphdr *ih;
263
264 switch (iph->protocol) {
265 case IPPROTO_TCP:
266 th = (void *)iph+iph->ihl*4;
267 sport = ntohs(th->source);
268 dport = ntohs(th->dest);
269 break;
270 case IPPROTO_UDP:
271 uh = (void *)iph+iph->ihl*4;
272 sport = ntohs(uh->source);
273 dport = ntohs(uh->dest);
274 break;
275 case IPPROTO_ICMP:
276 ih = (void *)iph+iph->ihl*4;
277 sport = ntohs(ih->un.echo.id);
278 dport = (ih->type<<8)|ih->code;
279 break;
280 default:
281 if (net_ratelimit()) {
282 printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",
283 iph->protocol);
284 }
285 sport = dport = 0;
286 }
287
288 switch (config->hash_mode) {
289 case CLUSTERIP_HASHMODE_SIP:
290 hashval = jhash_1word(ntohl(iph->saddr),
291 config->hash_initval);
292 break;
293 case CLUSTERIP_HASHMODE_SIP_SPT:
294 hashval = jhash_2words(ntohl(iph->saddr), sport,
295 config->hash_initval);
296 break;
297 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
298 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
299 config->hash_initval);
300 break;
301 default:
302 /* to make gcc happy */
303 hashval = 0;
304 /* This cannot happen, unless the check function wasn't called
305 * at rule load time */
306 printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);
307 BUG();
308 break;
309 }
310
311 /* node numbers are 1..n, not 0..n */
312 return ((hashval % config->num_total_nodes)+1);
313}
314
315static inline int
316clusterip_responsible(struct clusterip_config *config, u_int32_t hash)
317{
318 int i;
319
e45b1be8 320 read_lock_bh(&clusterip_lock);
1da177e4
LT
321
322 if (config->num_local_nodes == 0) {
e45b1be8 323 read_unlock_bh(&clusterip_lock);
1da177e4
LT
324 return 0;
325 }
326
327 for (i = 0; i < config->num_local_nodes; i++) {
328 if (config->local_nodes[i] == hash) {
e45b1be8 329 read_unlock_bh(&clusterip_lock);
1da177e4
LT
330 return 1;
331 }
332 }
333
e45b1be8 334 read_unlock_bh(&clusterip_lock);
1da177e4
LT
335
336 return 0;
337}
338
339/***********************************************************************
340 * IPTABLES TARGET
341 ***********************************************************************/
342
343static unsigned int
344target(struct sk_buff **pskb,
345 const struct net_device *in,
346 const struct net_device *out,
347 unsigned int hooknum,
348 const void *targinfo,
349 void *userinfo)
350{
351 const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
352 enum ip_conntrack_info ctinfo;
353 struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
354 u_int32_t hash;
355
356 /* don't need to clusterip_config_get() here, since refcount
357 * is only decremented by destroy() - and ip_tables guarantees
358 * that the ->target() function isn't called after ->destroy() */
359
360 if (!ct) {
361 printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
362 /* FIXME: need to drop invalid ones, since replies
363 * to outgoing connections of other nodes will be
364 * marked as INVALID */
365 return NF_DROP;
366 }
367
368 /* special case: ICMP error handling. conntrack distinguishes between
369 * error messages (RELATED) and information requests (see below) */
370 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP
371 && (ctinfo == IP_CT_RELATED
5d927eb0 372 || ctinfo == IP_CT_RELATED+IP_CT_IS_REPLY))
1da177e4
LT
373 return IPT_CONTINUE;
374
375 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
376 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
377 * on, which all have an ID field [relevant for hashing]. */
378
379 hash = clusterip_hashfn(*pskb, cipinfo->config);
380
381 switch (ctinfo) {
382 case IP_CT_NEW:
383 ct->mark = hash;
384 break;
385 case IP_CT_RELATED:
386 case IP_CT_RELATED+IP_CT_IS_REPLY:
387 /* FIXME: we don't handle expectations at the
388 * moment. they can arrive on a different node than
389 * the master connection (e.g. FTP passive mode) */
390 case IP_CT_ESTABLISHED:
391 case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:
392 break;
393 default:
394 break;
395 }
396
397#ifdef DEBUG_CLUSTERP
398 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
399#endif
bf3a46aa 400 DEBUGP("hash=%u ct_hash=%u ", hash, ct->mark);
1da177e4
LT
401 if (!clusterip_responsible(cipinfo->config, hash)) {
402 DEBUGP("not responsible\n");
403 return NF_DROP;
404 }
405 DEBUGP("responsible\n");
406
407 /* despite being received via linklayer multicast, this is
408 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
409 (*pskb)->pkt_type = PACKET_HOST;
410
411 return IPT_CONTINUE;
412}
413
414static int
415checkentry(const char *tablename,
416 const struct ipt_entry *e,
417 void *targinfo,
418 unsigned int targinfosize,
419 unsigned int hook_mask)
420{
421 struct ipt_clusterip_tgt_info *cipinfo = targinfo;
422
423 struct clusterip_config *config;
424
425 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info))) {
426 printk(KERN_WARNING "CLUSTERIP: targinfosize %u != %Zu\n",
427 targinfosize,
428 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)));
429 return 0;
430 }
431
432 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
433 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
434 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
435 printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",
436 cipinfo->hash_mode);
437 return 0;
438
439 }
440 if (e->ip.dmsk.s_addr != 0xffffffff
441 || e->ip.dst.s_addr == 0) {
442 printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
443 return 0;
444 }
445
446 /* FIXME: further sanity checks */
447
44513624
KK
448 config = clusterip_config_find_get(e->ip.dst.s_addr, 1);
449 if (config) {
450 if (cipinfo->config != NULL) {
451 /* Case A: This is an entry that gets reloaded, since
452 * it still has a cipinfo->config pointer. Simply
453 * increase the entry refcount and return */
454 if (cipinfo->config != config) {
455 printk(KERN_ERR "CLUSTERIP: Reloaded entry "
456 "has invalid config pointer!\n");
457 return 0;
458 }
459 clusterip_config_entry_get(cipinfo->config);
460 } else {
461 /* Case B: This is a new rule referring to an existing
462 * clusterip config. */
463 cipinfo->config = config;
464 clusterip_config_entry_get(cipinfo->config);
465 }
466 } else {
467 /* Case C: This is a completely new clusterip config */
1da177e4
LT
468 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
469 printk(KERN_WARNING "CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e->ip.dst.s_addr));
470 return 0;
471 } else {
472 struct net_device *dev;
473
474 if (e->ip.iniface[0] == '\0') {
475 printk(KERN_WARNING "CLUSTERIP: Please specify an interface name\n");
476 return 0;
477 }
478
479 dev = dev_get_by_name(e->ip.iniface);
480 if (!dev) {
481 printk(KERN_WARNING "CLUSTERIP: no such interface %s\n", e->ip.iniface);
482 return 0;
483 }
484
485 config = clusterip_config_init(cipinfo,
486 e->ip.dst.s_addr, dev);
487 if (!config) {
488 printk(KERN_WARNING "CLUSTERIP: cannot allocate config\n");
489 dev_put(dev);
490 return 0;
491 }
492 dev_mc_add(config->dev,config->clustermac, ETH_ALEN, 0);
493 }
44513624 494 cipinfo->config = config;
1da177e4
LT
495 }
496
1da177e4
LT
497 return 1;
498}
499
500/* drop reference count of cluster config when rule is deleted */
501static void destroy(void *matchinfo, unsigned int matchinfosize)
502{
503 struct ipt_clusterip_tgt_info *cipinfo = matchinfo;
504
44513624
KK
505 /* if no more entries are referencing the config, remove it
506 * from the list and destroy the proc entry */
507 clusterip_config_entry_put(cipinfo->config);
508
1da177e4
LT
509 clusterip_config_put(cipinfo->config);
510}
511
512static struct ipt_target clusterip_tgt = {
513 .name = "CLUSTERIP",
514 .target = &target,
515 .checkentry = &checkentry,
516 .destroy = &destroy,
517 .me = THIS_MODULE
518};
519
520
521/***********************************************************************
522 * ARP MANGLING CODE
523 ***********************************************************************/
524
525/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
526struct arp_payload {
527 u_int8_t src_hw[ETH_ALEN];
528 u_int32_t src_ip;
529 u_int8_t dst_hw[ETH_ALEN];
530 u_int32_t dst_ip;
531} __attribute__ ((packed));
532
533#ifdef CLUSTERIP_DEBUG
534static void arp_print(struct arp_payload *payload)
535{
536#define HBUFFERLEN 30
537 char hbuffer[HBUFFERLEN];
538 int j,k;
539 const char hexbuf[]= "0123456789abcdef";
540
541 for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
542 hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
543 hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
544 hbuffer[k++]=':';
545 }
546 hbuffer[--k]='\0';
547
548 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
549 NIPQUAD(payload->src_ip), hbuffer,
550 NIPQUAD(payload->dst_ip));
551}
552#endif
553
554static unsigned int
555arp_mangle(unsigned int hook,
556 struct sk_buff **pskb,
557 const struct net_device *in,
558 const struct net_device *out,
559 int (*okfn)(struct sk_buff *))
560{
561 struct arphdr *arp = (*pskb)->nh.arph;
562 struct arp_payload *payload;
563 struct clusterip_config *c;
564
565 /* we don't care about non-ethernet and non-ipv4 ARP */
566 if (arp->ar_hrd != htons(ARPHRD_ETHER)
567 || arp->ar_pro != htons(ETH_P_IP)
568 || arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
569 return NF_ACCEPT;
570
4095ebf1
HW
571 /* we only want to mangle arp requests and replies */
572 if (arp->ar_op != htons(ARPOP_REPLY)
573 && arp->ar_op != htons(ARPOP_REQUEST))
1da177e4
LT
574 return NF_ACCEPT;
575
576 payload = (void *)(arp+1);
577
578 /* if there is no clusterip configuration for the arp reply's
579 * source ip, we don't want to mangle it */
44513624 580 c = clusterip_config_find_get(payload->src_ip, 0);
1da177e4
LT
581 if (!c)
582 return NF_ACCEPT;
583
584 /* normally the linux kernel always replies to arp queries of
585 * addresses on different interfacs. However, in the CLUSTERIP case
586 * this wouldn't work, since we didn't subscribe the mcast group on
587 * other interfaces */
588 if (c->dev != out) {
589 DEBUGP("CLUSTERIP: not mangling arp reply on different "
590 "interface: cip'%s'-skb'%s'\n", c->dev->name, out->name);
591 clusterip_config_put(c);
592 return NF_ACCEPT;
593 }
594
595 /* mangle reply hardware address */
596 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
597
598#ifdef CLUSTERIP_DEBUG
599 DEBUGP(KERN_DEBUG "CLUSTERIP mangled arp reply: ");
600 arp_print(payload);
601#endif
602
603 clusterip_config_put(c);
604
605 return NF_ACCEPT;
606}
607
608static struct nf_hook_ops cip_arp_ops = {
609 .hook = arp_mangle,
610 .pf = NF_ARP,
611 .hooknum = NF_ARP_OUT,
612 .priority = -1
613};
614
615/***********************************************************************
616 * PROC DIR HANDLING
617 ***********************************************************************/
618
619#ifdef CONFIG_PROC_FS
620
621static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
622{
623 struct proc_dir_entry *pde = s->private;
624 struct clusterip_config *c = pde->data;
625 unsigned int *nodeidx;
626
e45b1be8 627 read_lock_bh(&clusterip_lock);
1da177e4
LT
628 if (*pos >= c->num_local_nodes)
629 return NULL;
630
631 nodeidx = kmalloc(sizeof(unsigned int), GFP_KERNEL);
632 if (!nodeidx)
633 return ERR_PTR(-ENOMEM);
634
635 *nodeidx = *pos;
636 return nodeidx;
637}
638
639static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
640{
641 struct proc_dir_entry *pde = s->private;
642 struct clusterip_config *c = pde->data;
643 unsigned int *nodeidx = (unsigned int *)v;
644
645 *pos = ++(*nodeidx);
646 if (*pos >= c->num_local_nodes) {
647 kfree(v);
648 return NULL;
649 }
650 return nodeidx;
651}
652
653static void clusterip_seq_stop(struct seq_file *s, void *v)
654{
655 kfree(v);
656
e45b1be8 657 read_unlock_bh(&clusterip_lock);
1da177e4
LT
658}
659
660static int clusterip_seq_show(struct seq_file *s, void *v)
661{
662 struct proc_dir_entry *pde = s->private;
663 struct clusterip_config *c = pde->data;
664 unsigned int *nodeidx = (unsigned int *)v;
665
666 if (*nodeidx != 0)
667 seq_putc(s, ',');
668 seq_printf(s, "%u", c->local_nodes[*nodeidx]);
669
670 if (*nodeidx == c->num_local_nodes-1)
671 seq_putc(s, '\n');
672
673 return 0;
674}
675
676static struct seq_operations clusterip_seq_ops = {
677 .start = clusterip_seq_start,
678 .next = clusterip_seq_next,
679 .stop = clusterip_seq_stop,
680 .show = clusterip_seq_show,
681};
682
683static int clusterip_proc_open(struct inode *inode, struct file *file)
684{
685 int ret = seq_open(file, &clusterip_seq_ops);
686
687 if (!ret) {
688 struct seq_file *sf = file->private_data;
689 struct proc_dir_entry *pde = PDE(inode);
690 struct clusterip_config *c = pde->data;
691
692 sf->private = pde;
693
694 clusterip_config_get(c);
695 }
696
697 return ret;
698}
699
700static int clusterip_proc_release(struct inode *inode, struct file *file)
701{
702 struct proc_dir_entry *pde = PDE(inode);
703 struct clusterip_config *c = pde->data;
704 int ret;
705
706 ret = seq_release(inode, file);
707
708 if (!ret)
709 clusterip_config_put(c);
710
711 return ret;
712}
713
714static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
715 size_t size, loff_t *ofs)
716{
717#define PROC_WRITELEN 10
718 char buffer[PROC_WRITELEN+1];
719 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
720 struct clusterip_config *c = pde->data;
721 unsigned long nodenum;
722
723 if (copy_from_user(buffer, input, PROC_WRITELEN))
724 return -EFAULT;
725
726 if (*buffer == '+') {
727 nodenum = simple_strtoul(buffer+1, NULL, 10);
728 if (clusterip_add_node(c, nodenum))
729 return -ENOMEM;
730 } else if (*buffer == '-') {
731 nodenum = simple_strtoul(buffer+1, NULL,10);
732 if (clusterip_del_node(c, nodenum))
733 return -ENOENT;
734 } else
735 return -EIO;
736
737 return size;
738}
739
740static struct file_operations clusterip_proc_fops = {
741 .owner = THIS_MODULE,
742 .open = clusterip_proc_open,
743 .read = seq_read,
744 .write = clusterip_proc_write,
745 .llseek = seq_lseek,
746 .release = clusterip_proc_release,
747};
748
749#endif /* CONFIG_PROC_FS */
750
751static int init_or_cleanup(int fini)
752{
753 int ret;
754
755 if (fini)
756 goto cleanup;
757
758 if (ipt_register_target(&clusterip_tgt)) {
759 ret = -EINVAL;
760 goto cleanup_none;
761 }
762
763 if (nf_register_hook(&cip_arp_ops) < 0) {
764 ret = -EINVAL;
765 goto cleanup_target;
766 }
767
768#ifdef CONFIG_PROC_FS
769 clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", proc_net);
770 if (!clusterip_procdir) {
771 printk(KERN_ERR "CLUSTERIP: Unable to proc dir entry\n");
772 ret = -ENOMEM;
773 goto cleanup_hook;
774 }
775#endif /* CONFIG_PROC_FS */
776
777 printk(KERN_NOTICE "ClusterIP Version %s loaded successfully\n",
778 CLUSTERIP_VERSION);
779
780 return 0;
781
782cleanup:
783 printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
784 CLUSTERIP_VERSION);
785#ifdef CONFIG_PROC_FS
786 remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
787#endif
788cleanup_hook:
789 nf_unregister_hook(&cip_arp_ops);
790cleanup_target:
791 ipt_unregister_target(&clusterip_tgt);
792cleanup_none:
793 return -EINVAL;
794}
795
796static int __init init(void)
797{
798 return init_or_cleanup(0);
799}
800
801static void __exit fini(void)
802{
803 init_or_cleanup(1);
804}
805
806module_init(init);
807module_exit(fini);