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