1
2
3
4
5
6
7
8
9
10
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13#include <linux/module.h>
14#include <linux/proc_fs.h>
15#include <linux/jhash.h>
16#include <linux/bitops.h>
17#include <linux/skbuff.h>
18#include <linux/slab.h>
19#include <linux/ip.h>
20#include <linux/tcp.h>
21#include <linux/udp.h>
22#include <linux/icmp.h>
23#include <linux/if_arp.h>
24#include <linux/seq_file.h>
25#include <linux/refcount.h>
26#include <linux/netfilter_arp.h>
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter_ipv4/ip_tables.h>
29#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
30#include <net/netfilter/nf_conntrack.h>
31#include <net/net_namespace.h>
32#include <net/netns/generic.h>
33#include <net/checksum.h>
34#include <net/ip.h>
35
36#define CLUSTERIP_VERSION "0.8"
37
38MODULE_LICENSE("GPL");
39MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
41
42struct clusterip_config {
43 struct list_head list;
44 refcount_t refcount;
45 refcount_t entries;
46
47
48 __be32 clusterip;
49 u_int8_t clustermac[ETH_ALEN];
50 int ifindex;
51 u_int16_t num_total_nodes;
52 unsigned long local_nodes;
53
54#ifdef CONFIG_PROC_FS
55 struct proc_dir_entry *pde;
56#endif
57 enum clusterip_hashmode hash_mode;
58 u_int32_t hash_initval;
59 struct rcu_head rcu;
60 struct net *net;
61 char ifname[IFNAMSIZ];
62};
63
64#ifdef CONFIG_PROC_FS
65static const struct file_operations clusterip_proc_fops;
66#endif
67
68struct clusterip_net {
69 struct list_head configs;
70
71 spinlock_t lock;
72
73#ifdef CONFIG_PROC_FS
74 struct proc_dir_entry *procdir;
75
76 struct mutex mutex;
77#endif
78};
79
80static unsigned int clusterip_net_id __read_mostly;
81static inline struct clusterip_net *clusterip_pernet(struct net *net)
82{
83 return net_generic(net, clusterip_net_id);
84}
85
86static inline void
87clusterip_config_get(struct clusterip_config *c)
88{
89 refcount_inc(&c->refcount);
90}
91
92static void clusterip_config_rcu_free(struct rcu_head *head)
93{
94 struct clusterip_config *config;
95 struct net_device *dev;
96
97 config = container_of(head, struct clusterip_config, rcu);
98 dev = dev_get_by_name(config->net, config->ifname);
99 if (dev) {
100 dev_mc_del(dev, config->clustermac);
101 dev_put(dev);
102 }
103 kfree(config);
104}
105
106static inline void
107clusterip_config_put(struct clusterip_config *c)
108{
109 if (refcount_dec_and_test(&c->refcount))
110 call_rcu(&c->rcu, clusterip_config_rcu_free);
111}
112
113
114
115
116static inline void
117clusterip_config_entry_put(struct clusterip_config *c)
118{
119 struct clusterip_net *cn = clusterip_pernet(c->net);
120
121 local_bh_disable();
122 if (refcount_dec_and_lock(&c->entries, &cn->lock)) {
123 list_del_rcu(&c->list);
124 spin_unlock(&cn->lock);
125 local_bh_enable();
126
127
128
129#ifdef CONFIG_PROC_FS
130 mutex_lock(&cn->mutex);
131 if (cn->procdir)
132 proc_remove(c->pde);
133 mutex_unlock(&cn->mutex);
134#endif
135 return;
136 }
137 local_bh_enable();
138}
139
140static struct clusterip_config *
141__clusterip_config_find(struct net *net, __be32 clusterip)
142{
143 struct clusterip_config *c;
144 struct clusterip_net *cn = clusterip_pernet(net);
145
146 list_for_each_entry_rcu(c, &cn->configs, list) {
147 if (c->clusterip == clusterip)
148 return c;
149 }
150
151 return NULL;
152}
153
154static inline struct clusterip_config *
155clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
156{
157 struct clusterip_config *c;
158
159 rcu_read_lock_bh();
160 c = __clusterip_config_find(net, clusterip);
161 if (c) {
162#ifdef CONFIG_PROC_FS
163 if (!c->pde)
164 c = NULL;
165 else
166#endif
167 if (unlikely(!refcount_inc_not_zero(&c->refcount)))
168 c = NULL;
169 else if (entry) {
170 if (unlikely(!refcount_inc_not_zero(&c->entries))) {
171 clusterip_config_put(c);
172 c = NULL;
173 }
174 }
175 }
176 rcu_read_unlock_bh();
177
178 return c;
179}
180
181static void
182clusterip_config_init_nodelist(struct clusterip_config *c,
183 const struct ipt_clusterip_tgt_info *i)
184{
185 int n;
186
187 for (n = 0; n < i->num_local_nodes; n++)
188 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
189}
190
191static int
192clusterip_netdev_event(struct notifier_block *this, unsigned long event,
193 void *ptr)
194{
195 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
196 struct net *net = dev_net(dev);
197 struct clusterip_net *cn = clusterip_pernet(net);
198 struct clusterip_config *c;
199
200 spin_lock_bh(&cn->lock);
201 list_for_each_entry_rcu(c, &cn->configs, list) {
202 switch (event) {
203 case NETDEV_REGISTER:
204 if (!strcmp(dev->name, c->ifname)) {
205 c->ifindex = dev->ifindex;
206 dev_mc_add(dev, c->clustermac);
207 }
208 break;
209 case NETDEV_UNREGISTER:
210 if (dev->ifindex == c->ifindex) {
211 dev_mc_del(dev, c->clustermac);
212 c->ifindex = -1;
213 }
214 break;
215 case NETDEV_CHANGENAME:
216 if (!strcmp(dev->name, c->ifname)) {
217 c->ifindex = dev->ifindex;
218 dev_mc_add(dev, c->clustermac);
219 } else if (dev->ifindex == c->ifindex) {
220 dev_mc_del(dev, c->clustermac);
221 c->ifindex = -1;
222 }
223 break;
224 }
225 }
226 spin_unlock_bh(&cn->lock);
227
228 return NOTIFY_DONE;
229}
230
231static struct clusterip_config *
232clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
233 __be32 ip, const char *iniface)
234{
235 struct clusterip_net *cn = clusterip_pernet(net);
236 struct clusterip_config *c;
237 struct net_device *dev;
238 int err;
239
240 if (iniface[0] == '\0') {
241 pr_info("Please specify an interface name\n");
242 return ERR_PTR(-EINVAL);
243 }
244
245 c = kzalloc(sizeof(*c), GFP_ATOMIC);
246 if (!c)
247 return ERR_PTR(-ENOMEM);
248
249 dev = dev_get_by_name(net, iniface);
250 if (!dev) {
251 pr_info("no such interface %s\n", iniface);
252 kfree(c);
253 return ERR_PTR(-ENOENT);
254 }
255 c->ifindex = dev->ifindex;
256 strcpy(c->ifname, dev->name);
257 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
258 dev_mc_add(dev, c->clustermac);
259 dev_put(dev);
260
261 c->clusterip = ip;
262 c->num_total_nodes = i->num_total_nodes;
263 clusterip_config_init_nodelist(c, i);
264 c->hash_mode = i->hash_mode;
265 c->hash_initval = i->hash_initval;
266 c->net = net;
267 refcount_set(&c->refcount, 1);
268
269 spin_lock_bh(&cn->lock);
270 if (__clusterip_config_find(net, ip)) {
271 err = -EBUSY;
272 goto out_config_put;
273 }
274
275 list_add_rcu(&c->list, &cn->configs);
276 spin_unlock_bh(&cn->lock);
277
278#ifdef CONFIG_PROC_FS
279 {
280 char buffer[16];
281
282
283 sprintf(buffer, "%pI4", &ip);
284 mutex_lock(&cn->mutex);
285 c->pde = proc_create_data(buffer, 0600,
286 cn->procdir,
287 &clusterip_proc_fops, c);
288 mutex_unlock(&cn->mutex);
289 if (!c->pde) {
290 err = -ENOMEM;
291 goto err;
292 }
293 }
294#endif
295
296 refcount_set(&c->entries, 1);
297 return c;
298
299#ifdef CONFIG_PROC_FS
300err:
301#endif
302 spin_lock_bh(&cn->lock);
303 list_del_rcu(&c->list);
304out_config_put:
305 spin_unlock_bh(&cn->lock);
306 clusterip_config_put(c);
307 return ERR_PTR(err);
308}
309
310#ifdef CONFIG_PROC_FS
311static int
312clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
313{
314
315 if (nodenum == 0 ||
316 nodenum > c->num_total_nodes)
317 return 1;
318
319
320 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
321 return 1;
322
323 return 0;
324}
325
326static bool
327clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
328{
329 if (nodenum == 0 ||
330 nodenum > c->num_total_nodes)
331 return true;
332
333 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
334 return false;
335
336 return true;
337}
338#endif
339
340static inline u_int32_t
341clusterip_hashfn(const struct sk_buff *skb,
342 const struct clusterip_config *config)
343{
344 const struct iphdr *iph = ip_hdr(skb);
345 unsigned long hashval;
346 u_int16_t sport = 0, dport = 0;
347 int poff;
348
349 poff = proto_ports_offset(iph->protocol);
350 if (poff >= 0) {
351 const u_int16_t *ports;
352 u16 _ports[2];
353
354 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
355 if (ports) {
356 sport = ports[0];
357 dport = ports[1];
358 }
359 } else {
360 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
361 }
362
363 switch (config->hash_mode) {
364 case CLUSTERIP_HASHMODE_SIP:
365 hashval = jhash_1word(ntohl(iph->saddr),
366 config->hash_initval);
367 break;
368 case CLUSTERIP_HASHMODE_SIP_SPT:
369 hashval = jhash_2words(ntohl(iph->saddr), sport,
370 config->hash_initval);
371 break;
372 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
373 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
374 config->hash_initval);
375 break;
376 default:
377
378 hashval = 0;
379
380
381 pr_info("unknown mode %u\n", config->hash_mode);
382 BUG();
383 break;
384 }
385
386
387 return reciprocal_scale(hashval, config->num_total_nodes) + 1;
388}
389
390static inline int
391clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
392{
393 return test_bit(hash - 1, &config->local_nodes);
394}
395
396
397
398
399
400static unsigned int
401clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
402{
403 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
404 struct nf_conn *ct;
405 enum ip_conntrack_info ctinfo;
406 u_int32_t hash;
407
408
409
410
411
412 ct = nf_ct_get(skb, &ctinfo);
413 if (ct == NULL)
414 return NF_DROP;
415
416
417
418 if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
419 (ctinfo == IP_CT_RELATED ||
420 ctinfo == IP_CT_RELATED_REPLY))
421 return XT_CONTINUE;
422
423
424
425
426
427 hash = clusterip_hashfn(skb, cipinfo->config);
428
429 switch (ctinfo) {
430 case IP_CT_NEW:
431 ct->mark = hash;
432 break;
433 case IP_CT_RELATED:
434 case IP_CT_RELATED_REPLY:
435
436
437
438 case IP_CT_ESTABLISHED:
439 case IP_CT_ESTABLISHED_REPLY:
440 break;
441 default:
442 break;
443 }
444
445#ifdef DEBUG
446 nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
447#endif
448 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
449 if (!clusterip_responsible(cipinfo->config, hash)) {
450 pr_debug("not responsible\n");
451 return NF_DROP;
452 }
453 pr_debug("responsible\n");
454
455
456
457 skb->pkt_type = PACKET_HOST;
458
459 return XT_CONTINUE;
460}
461
462static int clusterip_tg_check(const struct xt_tgchk_param *par)
463{
464 struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
465 const struct ipt_entry *e = par->entryinfo;
466 struct clusterip_config *config;
467 int ret, i;
468
469 if (par->nft_compat) {
470 pr_err("cannot use CLUSTERIP target from nftables compat\n");
471 return -EOPNOTSUPP;
472 }
473
474 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
475 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
476 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
477 pr_info("unknown mode %u\n", cipinfo->hash_mode);
478 return -EINVAL;
479
480 }
481 if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
482 e->ip.dst.s_addr == 0) {
483 pr_info("Please specify destination IP\n");
484 return -EINVAL;
485 }
486 if (cipinfo->num_local_nodes > ARRAY_SIZE(cipinfo->local_nodes)) {
487 pr_info("bad num_local_nodes %u\n", cipinfo->num_local_nodes);
488 return -EINVAL;
489 }
490 for (i = 0; i < cipinfo->num_local_nodes; i++) {
491 if (cipinfo->local_nodes[i] - 1 >=
492 sizeof(config->local_nodes) * 8) {
493 pr_info("bad local_nodes[%d] %u\n",
494 i, cipinfo->local_nodes[i]);
495 return -EINVAL;
496 }
497 }
498
499 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
500 if (!config) {
501 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
502 pr_info("no config found for %pI4, need 'new'\n",
503 &e->ip.dst.s_addr);
504 return -EINVAL;
505 } else {
506 config = clusterip_config_init(par->net, cipinfo,
507 e->ip.dst.s_addr,
508 e->ip.iniface);
509 if (IS_ERR(config))
510 return PTR_ERR(config);
511 }
512 } else if (memcmp(&config->clustermac, &cipinfo->clustermac, ETH_ALEN))
513 return -EINVAL;
514
515 ret = nf_ct_netns_get(par->net, par->family);
516 if (ret < 0) {
517 pr_info("cannot load conntrack support for proto=%u\n",
518 par->family);
519 clusterip_config_entry_put(config);
520 clusterip_config_put(config);
521 return ret;
522 }
523
524 if (!par->net->xt.clusterip_deprecated_warning) {
525 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
526 "use xt_cluster instead\n");
527 par->net->xt.clusterip_deprecated_warning = true;
528 }
529
530 cipinfo->config = config;
531 return ret;
532}
533
534
535static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
536{
537 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
538
539
540
541 clusterip_config_entry_put(cipinfo->config);
542
543 clusterip_config_put(cipinfo->config);
544
545 nf_ct_netns_put(par->net, par->family);
546}
547
548#ifdef CONFIG_COMPAT
549struct compat_ipt_clusterip_tgt_info
550{
551 u_int32_t flags;
552 u_int8_t clustermac[6];
553 u_int16_t num_total_nodes;
554 u_int16_t num_local_nodes;
555 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
556 u_int32_t hash_mode;
557 u_int32_t hash_initval;
558 compat_uptr_t config;
559};
560#endif
561
562static struct xt_target clusterip_tg_reg __read_mostly = {
563 .name = "CLUSTERIP",
564 .family = NFPROTO_IPV4,
565 .target = clusterip_tg,
566 .checkentry = clusterip_tg_check,
567 .destroy = clusterip_tg_destroy,
568 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
569 .usersize = offsetof(struct ipt_clusterip_tgt_info, config),
570#ifdef CONFIG_COMPAT
571 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
572#endif
573 .me = THIS_MODULE
574};
575
576
577
578
579
580
581
582struct arp_payload {
583 u_int8_t src_hw[ETH_ALEN];
584 __be32 src_ip;
585 u_int8_t dst_hw[ETH_ALEN];
586 __be32 dst_ip;
587} __packed;
588
589#ifdef DEBUG
590static void arp_print(struct arp_payload *payload)
591{
592#define HBUFFERLEN 30
593 char hbuffer[HBUFFERLEN];
594 int j, k;
595
596 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
597 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
598 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
599 hbuffer[k++] = ':';
600 }
601 hbuffer[--k] = '\0';
602
603 pr_debug("src %pI4@%s, dst %pI4\n",
604 &payload->src_ip, hbuffer, &payload->dst_ip);
605}
606#endif
607
608static unsigned int
609arp_mangle(void *priv,
610 struct sk_buff *skb,
611 const struct nf_hook_state *state)
612{
613 struct arphdr *arp = arp_hdr(skb);
614 struct arp_payload *payload;
615 struct clusterip_config *c;
616 struct net *net = state->net;
617
618
619 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
620 arp->ar_pro != htons(ETH_P_IP) ||
621 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
622 return NF_ACCEPT;
623
624
625 if (arp->ar_op != htons(ARPOP_REPLY) &&
626 arp->ar_op != htons(ARPOP_REQUEST))
627 return NF_ACCEPT;
628
629 payload = (void *)(arp+1);
630
631
632
633 c = clusterip_config_find_get(net, payload->src_ip, 0);
634 if (!c)
635 return NF_ACCEPT;
636
637
638
639
640
641 if (c->ifindex != state->out->ifindex) {
642 pr_debug("not mangling arp reply on different interface: cip'%d'-skb'%d'\n",
643 c->ifindex, state->out->ifindex);
644 clusterip_config_put(c);
645 return NF_ACCEPT;
646 }
647
648
649 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
650
651#ifdef DEBUG
652 pr_debug("mangled arp reply: ");
653 arp_print(payload);
654#endif
655
656 clusterip_config_put(c);
657
658 return NF_ACCEPT;
659}
660
661static const struct nf_hook_ops cip_arp_ops = {
662 .hook = arp_mangle,
663 .pf = NFPROTO_ARP,
664 .hooknum = NF_ARP_OUT,
665 .priority = -1
666};
667
668
669
670
671
672#ifdef CONFIG_PROC_FS
673
674struct clusterip_seq_position {
675 unsigned int pos;
676 unsigned int weight;
677 unsigned int bit;
678 unsigned long val;
679};
680
681static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
682{
683 struct clusterip_config *c = s->private;
684 unsigned int weight;
685 u_int32_t local_nodes;
686 struct clusterip_seq_position *idx;
687
688
689 local_nodes = c->local_nodes;
690 weight = hweight32(local_nodes);
691 if (*pos >= weight)
692 return NULL;
693
694 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
695 if (!idx)
696 return ERR_PTR(-ENOMEM);
697
698 idx->pos = *pos;
699 idx->weight = weight;
700 idx->bit = ffs(local_nodes);
701 idx->val = local_nodes;
702 clear_bit(idx->bit - 1, &idx->val);
703
704 return idx;
705}
706
707static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
708{
709 struct clusterip_seq_position *idx = v;
710
711 *pos = ++idx->pos;
712 if (*pos >= idx->weight) {
713 kfree(v);
714 return NULL;
715 }
716 idx->bit = ffs(idx->val);
717 clear_bit(idx->bit - 1, &idx->val);
718 return idx;
719}
720
721static void clusterip_seq_stop(struct seq_file *s, void *v)
722{
723 if (!IS_ERR(v))
724 kfree(v);
725}
726
727static int clusterip_seq_show(struct seq_file *s, void *v)
728{
729 struct clusterip_seq_position *idx = v;
730
731 if (idx->pos != 0)
732 seq_putc(s, ',');
733
734 seq_printf(s, "%u", idx->bit);
735
736 if (idx->pos == idx->weight - 1)
737 seq_putc(s, '\n');
738
739 return 0;
740}
741
742static const struct seq_operations clusterip_seq_ops = {
743 .start = clusterip_seq_start,
744 .next = clusterip_seq_next,
745 .stop = clusterip_seq_stop,
746 .show = clusterip_seq_show,
747};
748
749static int clusterip_proc_open(struct inode *inode, struct file *file)
750{
751 int ret = seq_open(file, &clusterip_seq_ops);
752
753 if (!ret) {
754 struct seq_file *sf = file->private_data;
755 struct clusterip_config *c = PDE_DATA(inode);
756
757 sf->private = c;
758
759 clusterip_config_get(c);
760 }
761
762 return ret;
763}
764
765static int clusterip_proc_release(struct inode *inode, struct file *file)
766{
767 struct clusterip_config *c = PDE_DATA(inode);
768 int ret;
769
770 ret = seq_release(inode, file);
771
772 if (!ret)
773 clusterip_config_put(c);
774
775 return ret;
776}
777
778static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
779 size_t size, loff_t *ofs)
780{
781 struct clusterip_config *c = PDE_DATA(file_inode(file));
782#define PROC_WRITELEN 10
783 char buffer[PROC_WRITELEN+1];
784 unsigned long nodenum;
785 int rc;
786
787 if (size > PROC_WRITELEN)
788 return -EIO;
789 if (copy_from_user(buffer, input, size))
790 return -EFAULT;
791 buffer[size] = 0;
792
793 if (*buffer == '+') {
794 rc = kstrtoul(buffer+1, 10, &nodenum);
795 if (rc)
796 return rc;
797 if (clusterip_add_node(c, nodenum))
798 return -ENOMEM;
799 } else if (*buffer == '-') {
800 rc = kstrtoul(buffer+1, 10, &nodenum);
801 if (rc)
802 return rc;
803 if (clusterip_del_node(c, nodenum))
804 return -ENOENT;
805 } else
806 return -EIO;
807
808 return size;
809}
810
811static const struct file_operations clusterip_proc_fops = {
812 .open = clusterip_proc_open,
813 .read = seq_read,
814 .write = clusterip_proc_write,
815 .llseek = seq_lseek,
816 .release = clusterip_proc_release,
817};
818
819#endif
820
821static int clusterip_net_init(struct net *net)
822{
823 struct clusterip_net *cn = clusterip_pernet(net);
824 int ret;
825
826 INIT_LIST_HEAD(&cn->configs);
827
828 spin_lock_init(&cn->lock);
829
830 ret = nf_register_net_hook(net, &cip_arp_ops);
831 if (ret < 0)
832 return ret;
833
834#ifdef CONFIG_PROC_FS
835 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
836 if (!cn->procdir) {
837 nf_unregister_net_hook(net, &cip_arp_ops);
838 pr_err("Unable to proc dir entry\n");
839 return -ENOMEM;
840 }
841 mutex_init(&cn->mutex);
842#endif
843
844 return 0;
845}
846
847static void clusterip_net_exit(struct net *net)
848{
849#ifdef CONFIG_PROC_FS
850 struct clusterip_net *cn = clusterip_pernet(net);
851
852 mutex_lock(&cn->mutex);
853 proc_remove(cn->procdir);
854 cn->procdir = NULL;
855 mutex_unlock(&cn->mutex);
856#endif
857 nf_unregister_net_hook(net, &cip_arp_ops);
858}
859
860static struct pernet_operations clusterip_net_ops = {
861 .init = clusterip_net_init,
862 .exit = clusterip_net_exit,
863 .id = &clusterip_net_id,
864 .size = sizeof(struct clusterip_net),
865};
866
867static struct notifier_block cip_netdev_notifier = {
868 .notifier_call = clusterip_netdev_event
869};
870
871static int __init clusterip_tg_init(void)
872{
873 int ret;
874
875 ret = register_pernet_subsys(&clusterip_net_ops);
876 if (ret < 0)
877 return ret;
878
879 ret = xt_register_target(&clusterip_tg_reg);
880 if (ret < 0)
881 goto cleanup_subsys;
882
883 ret = register_netdevice_notifier(&cip_netdev_notifier);
884 if (ret < 0)
885 goto unregister_target;
886
887 pr_info("ClusterIP Version %s loaded successfully\n",
888 CLUSTERIP_VERSION);
889
890 return 0;
891
892unregister_target:
893 xt_unregister_target(&clusterip_tg_reg);
894cleanup_subsys:
895 unregister_pernet_subsys(&clusterip_net_ops);
896 return ret;
897}
898
899static void __exit clusterip_tg_exit(void)
900{
901 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
902
903 unregister_netdevice_notifier(&cip_netdev_notifier);
904 xt_unregister_target(&clusterip_tg_reg);
905 unregister_pernet_subsys(&clusterip_net_ops);
906
907
908 rcu_barrier();
909}
910
911module_init(clusterip_tg_init);
912module_exit(clusterip_tg_exit);
913