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