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