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